home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / binutils.252 / gas / targ-cpu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-17  |  77.8 KB  |  2,888 lines

  1. /* i386.c -- Assemble code for the Intel 80386
  2.    Copyright (C) 1989, 1991, 1992, 1993 Free Software Foundation.
  3.  
  4.    This file is part of GAS, the GNU Assembler.
  5.  
  6.    GAS is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    GAS is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with GAS; see the file COPYING.  If not, write to
  18.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.   Intel 80386 machine specific gas.
  22.   Written by Eliot Dresselhaus (eliot@mgm.mit.edu).
  23.   Bugs & suggestions are completely welcome.  This is free software.
  24.   Please help us make it better.
  25.   */
  26.  
  27. #include <ctype.h>
  28.  
  29. #include "as.h"
  30.  
  31. #include "obstack.h"
  32. #include "opcode/i386.h"
  33.  
  34. #ifndef TC_RELOC
  35. #define TC_RELOC(X,Y) (Y)
  36. #endif
  37.  
  38. /* 'md_assemble ()' gathers together information and puts it into a
  39.    i386_insn. */
  40.  
  41. struct _i386_insn
  42.   {
  43.     /* TM holds the template for the insn were currently assembling. */
  44.     template tm;
  45.     /* SUFFIX holds the opcode suffix (e.g. 'l' for 'movl') if given. */
  46.     char suffix;
  47.     /* Operands are coded with OPERANDS, TYPES, DISPS, IMMS, and REGS. */
  48.  
  49.     /* OPERANDS gives the number of given operands. */
  50.     unsigned int operands;
  51.  
  52.     /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
  53.        of given register, displacement, memory operands and immediate
  54.        operands. */
  55.     unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
  56.  
  57.     /* TYPES [i] is the type (see above #defines) which tells us how to
  58.        search through DISPS [i] & IMMS [i] & REGS [i] for the required
  59.        operand.  */
  60.     unsigned int types[MAX_OPERANDS];
  61.  
  62.     /* Displacements (if given) for each operand. */
  63.     expressionS *disps[MAX_OPERANDS];
  64.  
  65.     /* Relocation type for operand */
  66. #ifdef BFD_ASSEMBLER
  67.     enum bfd_reloc_code_real disp_reloc[MAX_OPERANDS];
  68. #else
  69.     int disp_reloc[MAX_OPERANDS];
  70. #endif
  71.  
  72.     /* Immediate operands (if given) for each operand. */
  73.     expressionS *imms[MAX_OPERANDS];
  74.  
  75.     /* Register operands (if given) for each operand. */
  76.     reg_entry *regs[MAX_OPERANDS];
  77.  
  78.     /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
  79.        the base index byte below.  */
  80.     reg_entry *base_reg;
  81.     reg_entry *index_reg;
  82.     unsigned int log2_scale_factor;
  83.  
  84.     /* SEG gives the seg_entry of this insn.  It is equal to zero unless
  85.        an explicit segment override is given. */
  86.     const seg_entry *seg;    /* segment for memory operands (if given) */
  87.  
  88.     /* PREFIX holds all the given prefix opcodes (usually null).
  89.        PREFIXES is the size of PREFIX. */
  90.     /* richfix: really unsigned? */
  91.     unsigned char prefix[MAX_PREFIXES];
  92.     unsigned int prefixes;
  93.  
  94.     /* RM and IB are the modrm byte and the base index byte where the
  95.        addressing modes of this insn are encoded. */
  96.  
  97.     modrm_byte rm;
  98.     base_index_byte bi;
  99.   };
  100.  
  101. typedef struct _i386_insn i386_insn;
  102.  
  103. /* This array holds the chars that always start a comment.  If the
  104.    pre-processor is disabled, these aren't very useful */
  105. #if defined (TE_I386AIX) || defined (OBJ_ELF)
  106. const char comment_chars[] = "#/";
  107. #else
  108. const char comment_chars[] = "#";
  109. #endif
  110.  
  111. /* This array holds the chars that only start a comment at the beginning of
  112.    a line.  If the line seems to have the form '# 123 filename'
  113.    .line and .file directives will appear in the pre-processed output */
  114. /* Note that input_file.c hand checks for '#' at the beginning of the
  115.    first line of the input file.  This is because the compiler outputs
  116.    #NO_APP at the beginning of its output. */
  117. /* Also note that comments started like this one will always work if
  118.    '/' isn't otherwise defined.  */
  119. #if defined (TE_I386AIX) || defined (OBJ_ELF)
  120. const char line_comment_chars[] = "";
  121. #else
  122. const char line_comment_chars[] = "/";
  123. #endif
  124. const char line_separator_chars[] = "";
  125.  
  126. /* Chars that can be used to separate mant from exp in floating point nums */
  127. const char EXP_CHARS[] = "eE";
  128.  
  129. /* Chars that mean this number is a floating point constant */
  130. /* As in 0f12.456 */
  131. /* or    0d1.2345e12 */
  132. const char FLT_CHARS[] = "fFdDxX";
  133.  
  134. /* tables for lexical analysis */
  135. static char opcode_chars[256];
  136. static char register_chars[256];
  137. static char operand_chars[256];
  138. static char space_chars[256];
  139. static char identifier_chars[256];
  140. static char digit_chars[256];
  141.  
  142. /* lexical macros */
  143. #define is_opcode_char(x) (opcode_chars[(unsigned char) x])
  144. #define is_operand_char(x) (operand_chars[(unsigned char) x])
  145. #define is_register_char(x) (register_chars[(unsigned char) x])
  146. #define is_space_char(x) (space_chars[(unsigned char) x])
  147. #define is_identifier_char(x) (identifier_chars[(unsigned char) x])
  148. #define is_digit_char(x) (digit_chars[(unsigned char) x])
  149.  
  150. /* put here all non-digit non-letter charcters that may occur in an operand */
  151. static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
  152.  
  153. static char *ordinal_names[] = {"first", "second", "third"}; /* for printfs */
  154.  
  155. /* md_assemble() always leaves the strings it's passed unaltered.  To
  156.    effect this we maintain a stack of saved characters that we've smashed
  157.    with '\0's (indicating end of strings for various sub-fields of the
  158.    assembler instruction). */
  159. static char save_stack[32];
  160. static char *save_stack_p;    /* stack pointer */
  161. #define END_STRING_AND_SAVE(s)      *save_stack_p++ = *s; *s = '\0'
  162. #define RESTORE_END_STRING(s)       *s = *--save_stack_p
  163.  
  164. /* The instruction we're assembling. */
  165. static i386_insn i;
  166.  
  167. /* Per instruction expressionS buffers: 2 displacements & 2 immediate max. */
  168. static expressionS disp_expressions[2], im_expressions[2];
  169.  
  170. /* pointers to ebp & esp entries in reg_hash hash table */
  171. static reg_entry *ebp, *esp;
  172.  
  173. static int this_operand;    /* current operand we are working on */
  174.  
  175. static int flag_do_long_jump;    /* FIXME what does this do? */
  176.  
  177. /* Interface to relax_segment.
  178.    There are 2 relax states for 386 jump insns: one for conditional &
  179.    one for unconditional jumps.  This is because the these two types
  180.    of jumps add different sizes to frags when we're figuring out what
  181.    sort of jump to choose to reach a given label.  */
  182.  
  183. /* types */
  184. #define COND_JUMP 1        /* conditional jump */
  185. #define UNCOND_JUMP 2        /* unconditional jump */
  186. /* sizes */
  187. #define BYTE 0
  188. #define WORD 1
  189. #define DWORD 2
  190. #define UNKNOWN_SIZE 3
  191.  
  192. #ifndef INLINE
  193. #ifdef __GNUC__
  194. #define INLINE __inline__
  195. #else
  196. #define INLINE
  197. #endif
  198. #endif
  199.  
  200. #define ENCODE_RELAX_STATE(type,size) \
  201.   ((relax_substateT)((type<<2) | (size)))
  202. #define SIZE_FROM_RELAX_STATE(s) \
  203.     ( (((s) & 0x3) == BYTE ? 1 : (((s) & 0x3) == WORD ? 2 : 4)) )
  204.  
  205. const relax_typeS md_relax_table[] =
  206. {
  207. /* The fields are:
  208.    1) most positive reach of this state,
  209.    2) most negative reach of this state,
  210.    3) how many bytes this mode will add to the size of the current frag
  211.    4) which index into the table to try if we can't fit into this one.
  212.    */
  213.   {1, 1, 0, 0},
  214.   {1, 1, 0, 0},
  215.   {1, 1, 0, 0},
  216.   {1, 1, 0, 0},
  217.  
  218.   /* For now we don't use word displacement jumps; they may be
  219.      untrustworthy. */
  220.   {127 + 1, -128 + 1, 0, ENCODE_RELAX_STATE (COND_JUMP, DWORD)},
  221.   /* word conditionals add 3 bytes to frag:
  222.      2 opcode prefix; 1 displacement bytes */
  223.   {32767 + 2, -32768 + 2, 3, ENCODE_RELAX_STATE (COND_JUMP, DWORD)},
  224.   /* dword conditionals adds 4 bytes to frag:
  225.      1 opcode prefix; 3 displacement bytes */
  226.   {0, 0, 4, 0},
  227.   {1, 1, 0, 0},
  228.  
  229.   {127 + 1, -128 + 1, 0, ENCODE_RELAX_STATE (UNCOND_JUMP, DWORD)},
  230.   /* word jmp adds 2 bytes to frag:
  231.      1 opcode prefix; 1 displacement bytes */
  232.   {32767 + 2, -32768 + 2, 2, ENCODE_RELAX_STATE (UNCOND_JUMP, DWORD)},
  233.   /* dword jmp adds 3 bytes to frag:
  234.      0 opcode prefix; 3 displacement bytes */
  235.   {0, 0, 3, 0},
  236.   {1, 1, 0, 0},
  237.  
  238. };
  239.  
  240. static char *output_invalid PARAMS ((int c));
  241. static int i386_operand PARAMS ((char *operand_string));
  242. static reg_entry *parse_register PARAMS ((char *reg_string));
  243. #ifndef I386COFF
  244. static void s_bss PARAMS ((int));
  245. #endif
  246.  
  247. symbolS *GOT_symbol;        /* Pre-defined "__GLOBAL_OFFSET_TABLE" */
  248.  
  249. static INLINE unsigned long
  250. mode_from_disp_size (t)
  251.      unsigned long t;
  252. {
  253.   return (t & Disp8) ? 1 : (t & Disp32) ? 2 : 0;
  254. }
  255.  
  256. #if 0
  257. /* Not used.  */
  258. /* convert opcode suffix ('b' 'w' 'l' typically) into type specifier */
  259.  
  260. static INLINE unsigned long
  261. opcode_suffix_to_type (s)
  262.      unsigned long s;
  263. {
  264.   return (s == BYTE_OPCODE_SUFFIX
  265.       ? Byte : (s == WORD_OPCODE_SUFFIX
  266.             ? Word : DWord));
  267. }                /* opcode_suffix_to_type() */
  268. #endif
  269.  
  270. static INLINE int
  271. fits_in_signed_byte (num)
  272.      long num;
  273. {
  274.   return (num >= -128) && (num <= 127);
  275. }                /* fits_in_signed_byte() */
  276.  
  277. static INLINE int
  278. fits_in_unsigned_byte (num)
  279.      long num;
  280. {
  281.   return (num & 0xff) == num;
  282. }                /* fits_in_unsigned_byte() */
  283.  
  284. static INLINE int
  285. fits_in_unsigned_word (num)
  286.      long num;
  287. {
  288.   return (num & 0xffff) == num;
  289. }                /* fits_in_unsigned_word() */
  290.  
  291. static INLINE int
  292. fits_in_signed_word (num)
  293.      long num;
  294. {
  295.   return (-32768 <= num) && (num <= 32767);
  296. }                /* fits_in_signed_word() */
  297.  
  298. static int
  299. smallest_imm_type (num)
  300.      long num;
  301. {
  302. #if 0
  303.   /* This code is disabled because all the Imm1 forms in the opcode table
  304.      are slower on the i486, and they're the versions with the implicitly
  305.      specified single-position displacement, which has another syntax if
  306.      you really want to use that form.  If you really prefer to have the
  307.      one-byte-shorter Imm1 form despite these problems, re-enable this
  308.      code.  */
  309.   if (num == 1)
  310.     return Imm1 | Imm8 | Imm8S | Imm16 | Imm32;
  311. #endif
  312.   return (fits_in_signed_byte (num)
  313.       ? (Imm8S | Imm8 | Imm16 | Imm32)
  314.       : fits_in_unsigned_byte (num)
  315.       ? (Imm8 | Imm16 | Imm32)
  316.       : (fits_in_signed_word (num) || fits_in_unsigned_word (num))
  317.       ? (Imm16 | Imm32)
  318.       : (Imm32));
  319. }                /* smallest_imm_type() */
  320.  
  321. const pseudo_typeS md_pseudo_table[] =
  322. {
  323. #ifndef I386COFF
  324.   {"bss", s_bss, 0},
  325. #endif
  326. #ifndef OBJ_AOUT
  327.   {"align", s_align_bytes, 0},
  328. #else
  329.   {"align", s_align_ptwo, 0},
  330. #endif
  331.   {"ffloat", float_cons, 'f'},
  332.   {"dfloat", float_cons, 'd'},
  333.   {"tfloat", float_cons, 'x'},
  334.   {"value", cons, 2},
  335.   {"noopt", s_ignore, 0},
  336.   {"optim", s_ignore, 0},
  337.   {0, 0, 0}
  338. };
  339.  
  340. /* for interface with expression () */
  341. extern char *input_line_pointer;
  342.  
  343. /* obstack for constructing various things in md_begin */
  344. struct obstack o;
  345.  
  346. /* hash table for opcode lookup */
  347. static struct hash_control *op_hash;
  348. /* hash table for register lookup */
  349. static struct hash_control *reg_hash;
  350. /* hash table for prefix lookup */
  351. static struct hash_control *prefix_hash;
  352.  
  353.  
  354. void
  355. md_begin ()
  356. {
  357.   const char *hash_err;
  358.  
  359.   obstack_begin (&o, 4096);
  360.  
  361.   /* initialize op_hash hash table */
  362.   op_hash = hash_new ();
  363.  
  364.   {
  365.     register const template *optab;
  366.     register templates *core_optab;
  367.     char *prev_name;
  368.  
  369.     optab = i386_optab;        /* setup for loop */
  370.     prev_name = optab->name;
  371.     obstack_grow (&o, optab, sizeof (template));
  372.     core_optab = (templates *) xmalloc (sizeof (templates));
  373.  
  374.     for (optab++; optab < i386_optab_end; optab++)
  375.       {
  376.     if (!strcmp (optab->name, prev_name))
  377.       {
  378.         /* same name as before --> append to current template list */
  379.         obstack_grow (&o, optab, sizeof (template));
  380.       }
  381.     else
  382.       {
  383.         /* different name --> ship out current template list;
  384.            add to hash table; & begin anew */
  385.         /* Note: end must be set before start! since obstack_next_free
  386.            changes upon opstack_finish */
  387.         core_optab->end = (template *) obstack_next_free (&o);
  388.         core_optab->start = (template *) obstack_finish (&o);
  389.         hash_err = hash_insert (op_hash, prev_name, (char *) core_optab);
  390.         if (hash_err)
  391.           {
  392.           hash_error:
  393.         as_fatal ("Internal Error:  Can't hash %s: %s", prev_name,
  394.               hash_err);
  395.           }
  396.         prev_name = optab->name;
  397.         core_optab = (templates *) xmalloc (sizeof (templates));
  398.         obstack_grow (&o, optab, sizeof (template));
  399.       }
  400.       }
  401.   }
  402.  
  403.   /* initialize reg_hash hash table */
  404.   reg_hash = hash_new ();
  405.   {
  406.     register const reg_entry *regtab;
  407.  
  408.     for (regtab = i386_regtab; regtab < i386_regtab_end; regtab++)
  409.       {
  410.     hash_err = hash_insert (reg_hash, regtab->reg_name, (PTR) regtab);
  411.     if (hash_err)
  412.       goto hash_error;
  413.       }
  414.   }
  415.  
  416.   esp = (reg_entry *) hash_find (reg_hash, "esp");
  417.   ebp = (reg_entry *) hash_find (reg_hash, "ebp");
  418.  
  419.   /* initialize reg_hash hash table */
  420.   prefix_hash = hash_new ();
  421.   {
  422.     register const prefix_entry *prefixtab;
  423.  
  424.     for (prefixtab = i386_prefixtab;
  425.      prefixtab < i386_prefixtab_end; prefixtab++)
  426.       {
  427.     hash_err = hash_insert (prefix_hash, prefixtab->prefix_name,
  428.                 (PTR) prefixtab);
  429.     if (hash_err)
  430.       goto hash_error;
  431.       }
  432.   }
  433.  
  434.   /* fill in lexical tables:  opcode_chars, operand_chars, space_chars */
  435.   {
  436.     register int c;
  437.     register char *p;
  438.  
  439.     for (c = 0; c < 256; c++)
  440.       {
  441.     if (islower (c) || isdigit (c))
  442.       {
  443.         opcode_chars[c] = c;
  444.         register_chars[c] = c;
  445.       }
  446.     else if (isupper (c))
  447.       {
  448.         opcode_chars[c] = tolower (c);
  449.         register_chars[c] = opcode_chars[c];
  450.       }
  451.     else if (c == PREFIX_SEPERATOR)
  452.       {
  453.         opcode_chars[c] = c;
  454.       }
  455.     else if (c == ')' || c == '(')
  456.       {
  457.         register_chars[c] = c;
  458.       }
  459.  
  460.     if (isupper (c) || islower (c) || isdigit (c))
  461.       operand_chars[c] = c;
  462.  
  463.     if (isdigit (c) || c == '-')
  464.       digit_chars[c] = c;
  465.  
  466.     if (isalpha (c) || c == '_' || c == '.' || isdigit (c))
  467.       identifier_chars[c] = c;
  468.  
  469.     if (c == ' ' || c == '\t')
  470.       space_chars[c] = c;
  471.       }
  472.  
  473.     for (p = operand_special_chars; *p != '\0'; p++)
  474.       operand_chars[(unsigned char) *p] = *p;
  475.   }
  476.  
  477. #ifdef OBJ_ELF
  478.   record_alignment (text_section, 2);
  479.   record_alignment (data_section, 2);
  480.   record_alignment (bss_section, 2);
  481. #endif
  482. }
  483.  
  484.  
  485. #ifdef DEBUG386
  486.  
  487. /* debugging routines for md_assemble */
  488. static void pi PARAMS ((char *, i386_insn *));
  489. static void pte PARAMS ((template *));
  490. static void pt PARAMS ((unsigned int));
  491. static void pe PARAMS ((expressionS *));
  492. static void ps PARAMS ((symbolS *));
  493.  
  494. static void
  495. pi (line, x)
  496.      char *line;
  497.      i386_insn *x;
  498. {
  499.   register template *p;
  500.   int i;
  501.  
  502.   fprintf (stdout, "%s: template ", line);
  503.   pte (&x->tm);
  504.   fprintf (stdout, "  modrm:  mode %x  reg %x  reg/mem %x",
  505.        x->rm.mode, x->rm.reg, x->rm.regmem);
  506.   fprintf (stdout, " base %x  index %x  scale %x\n",
  507.        x->bi.base, x->bi.index, x->bi.scale);
  508.   for (i = 0; i < x->operands; i++)
  509.     {
  510.       fprintf (stdout, "    #%d:  ", i + 1);
  511.       pt (x->types[i]);
  512.       fprintf (stdout, "\n");
  513.       if (x->types[i] & Reg)
  514.     fprintf (stdout, "%s\n", x->regs[i]->reg_name);
  515.       if (x->types[i] & Imm)
  516.     pe (x->imms[i]);
  517.       if (x->types[i] & (Disp | Abs))
  518.     pe (x->disps[i]);
  519.     }
  520. }
  521.  
  522. static void
  523. pte (t)
  524.      template *t;
  525. {
  526.   int i;
  527.   fprintf (stdout, " %d operands ", t->operands);
  528.   fprintf (stdout, "opcode %x ",
  529.        t->base_opcode);
  530.   if (t->extension_opcode != None)
  531.     fprintf (stdout, "ext %x ", t->extension_opcode);
  532.   if (t->opcode_modifier & D)
  533.     fprintf (stdout, "D");
  534.   if (t->opcode_modifier & W)
  535.     fprintf (stdout, "W");
  536.   fprintf (stdout, "\n");
  537.   for (i = 0; i < t->operands; i++)
  538.     {
  539.       fprintf (stdout, "    #%d type ", i + 1);
  540.       pt (t->operand_types[i]);
  541.       fprintf (stdout, "\n");
  542.     }
  543. }
  544.  
  545. static void
  546. pe (e)
  547.      expressionS *e;
  548. {
  549.   fprintf (stdout, "    operation       %d\n", e->X_op);
  550.   fprintf (stdout, "    add_number    %d (%x)\n",
  551.        e->X_add_number, e->X_add_number);
  552.   if (e->X_add_symbol)
  553.     {
  554.       fprintf (stdout, "    add_symbol    ");
  555.       ps (e->X_add_symbol);
  556.       fprintf (stdout, "\n");
  557.     }
  558.   if (e->X_op_symbol)
  559.     {
  560.       fprintf (stdout, "    op_symbol    ");
  561.       ps (e->X_op_symbol);
  562.       fprintf (stdout, "\n");
  563.     }
  564. }
  565.  
  566. static void
  567. ps (s)
  568.      symbolS *s;
  569. {
  570.   fprintf (stdout, "%s type %s%s",
  571.        S_GET_NAME (s),
  572.        S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
  573.        segment_name (S_GET_SEGMENT (s)));
  574. }
  575.  
  576. struct type_name
  577.   {
  578.     unsigned int mask;
  579.     char *tname;
  580.   }
  581.  
  582. type_names[] =
  583. {
  584.   { Reg8, "r8" },
  585.   { Reg16, "r16" },
  586.   { Reg32, "r32" },
  587.   { Imm8, "i8" },
  588.   { Imm8S, "i8s" },
  589.   { Imm16, "i16" },
  590.   { Imm32, "i32" },
  591.   { Mem8, "Mem8" },
  592.   { Mem16, "Mem16" },
  593.   { Mem32, "Mem32" },
  594.   { BaseIndex, "BaseIndex" },
  595.   { Abs8, "Abs8" },
  596.   { Abs16, "Abs16" },
  597.   { Abs32, "Abs32" },
  598.   { Disp8, "d8" },
  599.   { Disp16, "d16" },
  600.   { Disp32, "d32" },
  601.   { SReg2, "SReg2" },
  602.   { SReg3, "SReg3" },
  603.   { Acc, "Acc" },
  604.   { InOutPortReg, "InOutPortReg" },
  605.   { ShiftCount, "ShiftCount" },
  606.   { Imm1, "i1" },
  607.   { Control, "control reg" },
  608.   { Test, "test reg" },
  609.   { FloatReg, "FReg" },
  610.   { FloatAcc, "FAcc" },
  611.   { JumpAbsolute, "Jump Absolute" },
  612.   { 0, "" }
  613. };
  614.  
  615. static void
  616. pt (t)
  617.      unsigned int t;
  618. {
  619.   register struct type_name *ty;
  620.  
  621.   if (t == Unknown)
  622.     {
  623.       fprintf (stdout, "Unknown");
  624.     }
  625.   else
  626.     {
  627.       for (ty = type_names; ty->mask; ty++)
  628.     if (t & ty->mask)
  629.       fprintf (stdout, "%s, ", ty->tname);
  630.     }
  631.   fflush (stdout);
  632. }
  633.  
  634. #endif /* DEBUG386 */
  635.  
  636. #ifdef BFD_ASSEMBLER
  637. static bfd_reloc_code_real_type
  638. reloc (size, pcrel, other)
  639.      int size;
  640.      int pcrel;
  641.      bfd_reloc_code_real_type other;
  642. {
  643.   if (other != NO_RELOC) return other;
  644.  
  645.   if (pcrel)
  646.     switch (size)
  647.       {
  648.       case 1: return BFD_RELOC_8_PCREL;
  649.       case 2: return BFD_RELOC_16_PCREL;
  650.       case 4: return BFD_RELOC_32_PCREL;
  651.       }
  652.   else
  653.     switch (size)
  654.       {
  655.       case 1: return BFD_RELOC_8;
  656.       case 2: return BFD_RELOC_16;
  657.       case 4: return BFD_RELOC_32;
  658.       }
  659.  
  660.   as_bad ("Can not do %d byte %srelocation", size,
  661.       pcrel ? "pc-relative " : "");
  662.   return BFD_RELOC_NONE;
  663. }
  664. #else
  665. #define reloc(SIZE,PCREL,OTHER)    0
  666. #define BFD_RELOC_32        0
  667. #define BFD_RELOC_32_PCREL    0
  668. #define BFD_RELOC_386_PLT32    0
  669. #define BFD_RELOC_386_GOT32    0
  670. #define BFD_RELOC_386_GOTOFF    0
  671. #endif
  672.  
  673. /*
  674.  * Here we decide which fixups can be adjusted to make them relative to
  675.  * the beginning of the section instead of the symbol.  Basically we need
  676.  * to make sure that the dynamic relocations are done correctly, so in
  677.  * some cases we force the original symbol to be used.
  678.  */
  679. tc_i386_fix_adjustable(fixP)
  680.      fixS * fixP;
  681. {
  682.   /* Prevent all adjustments to global symbols. */
  683.   if (!S_IS_LOCAL (fixP->fx_addsy))
  684.     return 0;
  685. #ifdef BFD_ASSEMBLER
  686.   /* adjust_reloc_syms doesn't know about the GOT */
  687.   if (fixP->fx_r_type == BFD_RELOC_386_GOTOFF)
  688.     return 0;
  689. #endif
  690.   return 1;
  691. }
  692.  
  693. /* This is the guts of the machine-dependent assembler.  LINE points to a
  694.    machine dependent instruction.  This function is supposed to emit
  695.    the frags/bytes it assembles to.  */
  696.  
  697. void
  698. md_assemble (line)
  699.      char *line;
  700. {
  701.   /* Holds template once we've found it. */
  702.   template *t;
  703.  
  704.   /* Count the size of the instruction generated.  */
  705.   int insn_size = 0;
  706.  
  707.   /* Possible templates for current insn */
  708.   templates *current_templates = (templates *) 0;
  709.  
  710.   int j;
  711.  
  712.   /* Initialize globals. */
  713.   memset (&i, '\0', sizeof (i));
  714.   for (j = 0; j < MAX_OPERANDS; j++)
  715.     i.disp_reloc[j] = NO_RELOC;
  716.   memset (disp_expressions, '\0', sizeof (disp_expressions));
  717.   memset (im_expressions, '\0', sizeof (im_expressions));
  718.   save_stack_p = save_stack;    /* reset stack pointer */
  719.  
  720.   /* Fist parse an opcode & call i386_operand for the operands.
  721.      We assume that the scrubber has arranged it so that line[0] is the valid
  722.      start of a (possibly prefixed) opcode. */
  723.   {
  724.     char *l = line;
  725.  
  726.     /* 1 if operand is pending after ','. */
  727.     unsigned int expecting_operand = 0;
  728.     /* 1 if we found a prefix only acceptable with string insns. */
  729.     unsigned int expecting_string_instruction = 0;
  730.     /* Non-zero if operand parens not balenced. */
  731.     unsigned int paren_not_balenced;
  732.     char *token_start = l;
  733.  
  734.     while (!is_space_char (*l) && *l != END_OF_INSN)
  735.       {
  736.     if (!is_opcode_char (*l))
  737.       {
  738.         as_bad ("invalid character %s in opcode", output_invalid (*l));
  739.         return;
  740.       }
  741.     else if (*l != PREFIX_SEPERATOR)
  742.       {
  743.         *l = opcode_chars[(unsigned char) *l];    /* fold case of opcodes */
  744.         l++;
  745.       }
  746.     else
  747.       {
  748.         /* This opcode's got a prefix.  */
  749.         unsigned int q;
  750.         prefix_entry *prefix;
  751.  
  752.         if (l == token_start)
  753.           {
  754.         as_bad ("expecting prefix; got nothing");
  755.         return;
  756.           }
  757.         END_STRING_AND_SAVE (l);
  758.         prefix = (prefix_entry *) hash_find (prefix_hash, token_start);
  759.         if (!prefix)
  760.           {
  761.         as_bad ("no such opcode prefix ('%s')", token_start);
  762.         return;
  763.           }
  764.         RESTORE_END_STRING (l);
  765.         /* check for repeated prefix */
  766.         for (q = 0; q < i.prefixes; q++)
  767.           if (i.prefix[q] == prefix->prefix_code)
  768.         {
  769.           as_bad ("same prefix used twice; you don't really want this!");
  770.           return;
  771.         }
  772.         if (i.prefixes == MAX_PREFIXES)
  773.           {
  774.         as_bad ("too many opcode prefixes");
  775.         return;
  776.           }
  777.         i.prefix[i.prefixes++] = prefix->prefix_code;
  778.         if (prefix->prefix_code == REPE || prefix->prefix_code == REPNE)
  779.           expecting_string_instruction = 1;
  780.         /* skip past PREFIX_SEPERATOR and reset token_start */
  781.         token_start = ++l;
  782.       }
  783.       }
  784.     END_STRING_AND_SAVE (l);
  785.     if (token_start == l)
  786.       {
  787.     as_bad ("expecting opcode; got nothing");
  788.     return;
  789.       }
  790.  
  791.     /* Lookup insn in hash; try intel & att naming conventions if appropriate;
  792.        that is:  we only use the opcode suffix 'b' 'w' or 'l' if we need to. */
  793.     current_templates = (templates *) hash_find (op_hash, token_start);
  794.     if (!current_templates)
  795.       {
  796.     int last_index = strlen (token_start) - 1;
  797.     char last_char = token_start[last_index];
  798.     switch (last_char)
  799.       {
  800.       case DWORD_OPCODE_SUFFIX:
  801.       case WORD_OPCODE_SUFFIX:
  802.       case BYTE_OPCODE_SUFFIX:
  803.         token_start[last_index] = '\0';
  804.         current_templates = (templates *) hash_find (op_hash, token_start);
  805.         token_start[last_index] = last_char;
  806.         i.suffix = last_char;
  807.       }
  808.     if (!current_templates)
  809.       {
  810.         as_bad ("no such 386 instruction: `%s'", token_start);
  811.         return;
  812.       }
  813.       }
  814.     RESTORE_END_STRING (l);
  815.  
  816.     /* check for rep/repne without a string instruction */
  817.     if (expecting_string_instruction &&
  818.     !IS_STRING_INSTRUCTION (current_templates->
  819.                 start->base_opcode))
  820.       {
  821.     as_bad ("expecting string instruction after rep/repne");
  822.     return;
  823.       }
  824.  
  825.     /* There may be operands to parse. */
  826.     if (*l != END_OF_INSN &&
  827.     /* For string instructions, we ignore any operands if given.  This
  828.        kludges, for example, 'rep/movsb %ds:(%esi), %es:(%edi)' where
  829.        the operands are always going to be the same, and are not really
  830.        encoded in machine code. */
  831.     !IS_STRING_INSTRUCTION (current_templates->
  832.                 start->base_opcode))
  833.       {
  834.     /* parse operands */
  835.     do
  836.       {
  837.         /* skip optional white space before operand */
  838.         while (!is_operand_char (*l) && *l != END_OF_INSN)
  839.           {
  840.         if (!is_space_char (*l))
  841.           {
  842.             as_bad ("invalid character %s before %s operand",
  843.                 output_invalid (*l),
  844.                 ordinal_names[i.operands]);
  845.             return;
  846.           }
  847.         l++;
  848.           }
  849.         token_start = l;    /* after white space */
  850.         paren_not_balenced = 0;
  851.         while (paren_not_balenced || *l != ',')
  852.           {
  853.         if (*l == END_OF_INSN)
  854.           {
  855.             if (paren_not_balenced)
  856.               {
  857.             as_bad ("unbalenced parenthesis in %s operand.",
  858.                 ordinal_names[i.operands]);
  859.             return;
  860.               }
  861.             else
  862.               break;    /* we are done */
  863.           }
  864.         else if (!is_operand_char (*l) && !is_space_char (*l))
  865.           {
  866.             as_bad ("invalid character %s in %s operand",
  867.                 output_invalid (*l),
  868.                 ordinal_names[i.operands]);
  869.             return;
  870.           }
  871.         if (*l == '(')
  872.           ++paren_not_balenced;
  873.         if (*l == ')')
  874.           --paren_not_balenced;
  875.         l++;
  876.           }
  877.         if (l != token_start)
  878.           {            /* yes, we've read in another operand */
  879.         unsigned int operand_ok;
  880.         this_operand = i.operands++;
  881.         if (i.operands > MAX_OPERANDS)
  882.           {
  883.             as_bad ("spurious operands; (%d operands/instruction max)",
  884.                 MAX_OPERANDS);
  885.             return;
  886.           }
  887.         /* now parse operand adding info to 'i' as we go along */
  888.         END_STRING_AND_SAVE (l);
  889.         operand_ok = i386_operand (token_start);
  890.         RESTORE_END_STRING (l);    /* restore old contents */
  891.         if (!operand_ok)
  892.           return;
  893.           }
  894.         else
  895.           {
  896.         if (expecting_operand)
  897.           {
  898.           expecting_operand_after_comma:
  899.             as_bad ("expecting operand after ','; got nothing");
  900.             return;
  901.           }
  902.         if (*l == ',')
  903.           {
  904.             as_bad ("expecting operand before ','; got nothing");
  905.             return;
  906.           }
  907.           }
  908.  
  909.         /* now *l must be either ',' or END_OF_INSN */
  910.         if (*l == ',')
  911.           {
  912.         if (*++l == END_OF_INSN)
  913.           {        /* just skip it, if it's \n complain */
  914.             goto expecting_operand_after_comma;
  915.           }
  916.         expecting_operand = 1;
  917.           }
  918.       }
  919.     while (*l != END_OF_INSN);    /* until we get end of insn */
  920.       }
  921.   }
  922.  
  923.   /* Now we've parsed the opcode into a set of templates, and have the
  924.      operands at hand.
  925.  
  926.      Next, we find a template that matches the given insn,
  927.      making sure the overlap of the given operands types is consistent
  928.      with the template operand types. */
  929.  
  930. #define MATCH(overlap,given_type) \
  931.     (overlap && \
  932.      (((overlap & (JumpAbsolute|BaseIndex|Mem8)) \
  933.        == (given_type & (JumpAbsolute|BaseIndex|Mem8))) \
  934.       || (overlap == InOutPortReg)))
  935.  
  936.  
  937.   /* If m0 and m1 are register matches they must be consistent
  938.      with the expected operand types t0 and t1.
  939.      That is, if both m0 & m1 are register matches
  940.      i.e. ( ((m0 & (Reg)) && (m1 & (Reg)) ) ?
  941.      then, either 1. or 2. must be true:
  942.      1. the expected operand type register overlap is null:
  943.      (t0 & t1 & Reg) == 0
  944.      AND
  945.      the given register overlap is null:
  946.      (m0 & m1 & Reg) == 0
  947.      2. the expected operand type register overlap == the given
  948.      operand type overlap:  (t0 & t1 & m0 & m1 & Reg).
  949.      */
  950. #define CONSISTENT_REGISTER_MATCH(m0, m1, t0, t1) \
  951.         ( ((m0 & (Reg)) && (m1 & (Reg))) ? \
  952.          ( ((t0 & t1 & (Reg)) == 0 && (m0 & m1 & (Reg)) == 0) || \
  953.           ((t0 & t1) & (m0 & m1) & (Reg)) \
  954.           ) : 1)
  955.   {
  956.     register unsigned int overlap0, overlap1;
  957.     expressionS *exp;
  958.     unsigned int overlap2;
  959.     unsigned int found_reverse_match;
  960.  
  961.     overlap0 = overlap1 = overlap2 = found_reverse_match = 0;
  962.     for (t = current_templates->start;
  963.      t < current_templates->end;
  964.      t++)
  965.       {
  966.     /* must have right number of operands */
  967.     if (i.operands != t->operands)
  968.       continue;
  969.     else if (!t->operands)
  970.       break;        /* 0 operands always matches */
  971.  
  972.     overlap0 = i.types[0] & t->operand_types[0];
  973.     switch (t->operands)
  974.       {
  975.       case 1:
  976.         if (!MATCH (overlap0, i.types[0]))
  977.           continue;
  978.         break;
  979.       case 2:
  980.       case 3:
  981.         overlap1 = i.types[1] & t->operand_types[1];
  982.         if (!MATCH (overlap0, i.types[0]) ||
  983.         !MATCH (overlap1, i.types[1]) ||
  984.         !CONSISTENT_REGISTER_MATCH (overlap0, overlap1,
  985.                         t->operand_types[0],
  986.                         t->operand_types[1]))
  987.           {
  988.  
  989.         /* check if other direction is valid ... */
  990.         if (!(t->opcode_modifier & COMES_IN_BOTH_DIRECTIONS))
  991.           continue;
  992.  
  993.         /* try reversing direction of operands */
  994.         overlap0 = i.types[0] & t->operand_types[1];
  995.         overlap1 = i.types[1] & t->operand_types[0];
  996.         if (!MATCH (overlap0, i.types[0]) ||
  997.             !MATCH (overlap1, i.types[1]) ||
  998.             !CONSISTENT_REGISTER_MATCH (overlap0, overlap1,
  999.                         t->operand_types[0],
  1000.                         t->operand_types[1]))
  1001.           {
  1002.             /* does not match either direction */
  1003.             continue;
  1004.           }
  1005.         /* found a reverse match here -- slip through */
  1006.         /* found_reverse_match holds which of D or FloatD we've found */
  1007.         found_reverse_match = t->opcode_modifier & COMES_IN_BOTH_DIRECTIONS;
  1008.           }            /* endif: not forward match */
  1009.         /* found either forward/reverse 2 operand match here */
  1010.         if (t->operands == 3)
  1011.           {
  1012.         overlap2 = i.types[2] & t->operand_types[2];
  1013.         if (!MATCH (overlap2, i.types[2]) ||
  1014.             !CONSISTENT_REGISTER_MATCH (overlap0, overlap2,
  1015.                         t->operand_types[0],
  1016.                         t->operand_types[2]) ||
  1017.             !CONSISTENT_REGISTER_MATCH (overlap1, overlap2,
  1018.                         t->operand_types[1],
  1019.                         t->operand_types[2]))
  1020.           continue;
  1021.           }
  1022.         /* found either forward/reverse 2 or 3 operand match here:
  1023.            slip through to break */
  1024.       }
  1025.     break;            /* we've found a match; break out of loop */
  1026.       }                /* for (t = ... */
  1027.     if (t == current_templates->end)
  1028.       {                /* we found no match */
  1029.     as_bad ("operands given don't match any known 386 instruction");
  1030.     return;
  1031.       }
  1032.  
  1033.     /* Copy the template we found (we may change it!). */
  1034.     i.tm = *t;
  1035.     t = &i.tm;            /* alter new copy of template */
  1036.  
  1037.     /* If there's no opcode suffix we try to invent one based on register
  1038.        operands. */
  1039.     if (!i.suffix && i.reg_operands)
  1040.       {
  1041.     /* We take i.suffix from the LAST register operand specified.  This
  1042.        assumes that the last register operands is the destination register
  1043.        operand. */
  1044.     int op;
  1045.     for (op = 0; op < MAX_OPERANDS; op++)
  1046.       if (i.types[op] & Reg)
  1047.         {
  1048.           i.suffix = ((i.types[op] == Reg8) ? BYTE_OPCODE_SUFFIX :
  1049.               (i.types[op] == Reg16) ? WORD_OPCODE_SUFFIX :
  1050.               DWORD_OPCODE_SUFFIX);
  1051.         }
  1052.       }
  1053.  
  1054.     /* Make still unresolved immediate matches conform to size of immediate
  1055.        given in i.suffix. Note:  overlap2 cannot be an immediate!
  1056.        We assume this. */
  1057.     if ((overlap0 & (Imm8 | Imm8S | Imm16 | Imm32))
  1058.     && overlap0 != Imm8 && overlap0 != Imm8S
  1059.     && overlap0 != Imm16 && overlap0 != Imm32)
  1060.       {
  1061.     if (!i.suffix)
  1062.       {
  1063.         as_bad ("no opcode suffix given; can't determine immediate size");
  1064.         return;
  1065.       }
  1066.     overlap0 &= (i.suffix == BYTE_OPCODE_SUFFIX ? (Imm8 | Imm8S) :
  1067.              (i.suffix == WORD_OPCODE_SUFFIX ? Imm16 : Imm32));
  1068.       }
  1069.     if ((overlap1 & (Imm8 | Imm8S | Imm16 | Imm32))
  1070.     && overlap1 != Imm8 && overlap1 != Imm8S
  1071.     && overlap1 != Imm16 && overlap1 != Imm32)
  1072.       {
  1073.     if (!i.suffix)
  1074.       {
  1075.         as_bad ("no opcode suffix given; can't determine immediate size");
  1076.         return;
  1077.       }
  1078.     overlap1 &= (i.suffix == BYTE_OPCODE_SUFFIX ? (Imm8 | Imm8S) :
  1079.              (i.suffix == WORD_OPCODE_SUFFIX ? Imm16 : Imm32));
  1080.       }
  1081.  
  1082.     i.types[0] = overlap0;
  1083.     i.types[1] = overlap1;
  1084.     i.types[2] = overlap2;
  1085.  
  1086.     if (overlap0 & ImplicitRegister)
  1087.       i.reg_operands--;
  1088.     if (overlap1 & ImplicitRegister)
  1089.       i.reg_operands--;
  1090.     if (overlap2 & ImplicitRegister)
  1091.       i.reg_operands--;
  1092.     if (overlap0 & Imm1)
  1093.       i.imm_operands = 0;    /* kludge for shift insns */
  1094.  
  1095.     if (found_reverse_match)
  1096.       {
  1097.     unsigned int save;
  1098.     save = t->operand_types[0];
  1099.     t->operand_types[0] = t->operand_types[1];
  1100.     t->operand_types[1] = save;
  1101.       }
  1102.  
  1103.     /* Finalize opcode.  First, we change the opcode based on the operand
  1104.        size given by i.suffix: we never have to change things for byte insns,
  1105.        or when no opcode suffix is need to size the operands. */
  1106.  
  1107.     if (!i.suffix && (t->opcode_modifier & W))
  1108.       {
  1109.     as_bad ("no opcode suffix given and no register operands; can't size instruction");
  1110.     return;
  1111.       }
  1112.  
  1113.     if (i.suffix && i.suffix != BYTE_OPCODE_SUFFIX)
  1114.       {
  1115.     /* Select between byte and word/dword operations. */
  1116.     if (t->opcode_modifier & W)
  1117.       t->base_opcode |= W;
  1118.     /* Now select between word & dword operations via the
  1119.                    operand size prefix. */
  1120.     if (i.suffix == WORD_OPCODE_SUFFIX)
  1121.       {
  1122.         if (i.prefixes == MAX_PREFIXES)
  1123.           {
  1124.         as_bad ("%d prefixes given and 'w' opcode suffix gives too many prefixes",
  1125.             MAX_PREFIXES);
  1126.         return;
  1127.           }
  1128.         i.prefix[i.prefixes++] = WORD_PREFIX_OPCODE;
  1129.       }
  1130.       }
  1131.  
  1132.     /* For insns with operands there are more diddles to do to the opcode. */
  1133.     if (i.operands)
  1134.       {
  1135.     /* If we found a reverse match we must alter the opcode direction bit
  1136.        found_reverse_match holds bit to set (different for int &
  1137.        float insns). */
  1138.  
  1139.     if (found_reverse_match)
  1140.       {
  1141.         t->base_opcode |= found_reverse_match;
  1142.       }
  1143.  
  1144.     /* The imul $imm, %reg instruction is converted into
  1145.        imul $imm, %reg, %reg. */
  1146.     if (t->opcode_modifier & imulKludge)
  1147.       {
  1148.         /* Pretend we saw the 3 operand case. */
  1149.         i.regs[2] = i.regs[1];
  1150.         i.reg_operands = 2;
  1151.       }
  1152.  
  1153.     /* Certain instructions expect the destination to be in the i.rm.reg
  1154.        field.  This is by far the exceptional case.  For these
  1155.        instructions, if the source operand is a register, we must reverse
  1156.        the i.rm.reg and i.rm.regmem fields.  We accomplish this by faking
  1157.        that the two register operands were given in the reverse order. */
  1158.     if ((t->opcode_modifier & ReverseRegRegmem) && i.reg_operands == 2)
  1159.       {
  1160.         unsigned int first_reg_operand = (i.types[0] & Reg) ? 0 : 1;
  1161.         unsigned int second_reg_operand = first_reg_operand + 1;
  1162.         reg_entry *tmp = i.regs[first_reg_operand];
  1163.         i.regs[first_reg_operand] = i.regs[second_reg_operand];
  1164.         i.regs[second_reg_operand] = tmp;
  1165.       }
  1166.  
  1167.     if (t->opcode_modifier & ShortForm)
  1168.       {
  1169.         /* The register or float register operand is in operand 0 or 1. */
  1170.         unsigned int op = (i.types[0] & (Reg | FloatReg)) ? 0 : 1;
  1171.         /* Register goes in low 3 bits of opcode. */
  1172.         t->base_opcode |= i.regs[op]->reg_num;
  1173.       }
  1174.     else if (t->opcode_modifier & ShortFormW)
  1175.       {
  1176.         /* Short form with 0x8 width bit.  Register is always dest. operand */
  1177.         t->base_opcode |= i.regs[1]->reg_num;
  1178.         if (i.suffix == WORD_OPCODE_SUFFIX ||
  1179.         i.suffix == DWORD_OPCODE_SUFFIX)
  1180.           t->base_opcode |= 0x8;
  1181.       }
  1182.     else if (t->opcode_modifier & Seg2ShortForm)
  1183.       {
  1184.         if (t->base_opcode == POP_SEG_SHORT && i.regs[0]->reg_num == 1)
  1185.           {
  1186.         as_bad ("you can't 'pop cs' on the 386.");
  1187.         return;
  1188.           }
  1189.         t->base_opcode |= (i.regs[0]->reg_num << 3);
  1190.       }
  1191.     else if (t->opcode_modifier & Seg3ShortForm)
  1192.       {
  1193.         /* 'push %fs' is 0x0fa0; 'pop %fs' is 0x0fa1.
  1194.            'push %gs' is 0x0fa8; 'pop %fs' is 0x0fa9.
  1195.            So, only if i.regs[0]->reg_num == 5 (%gs) do we need
  1196.            to change the opcode. */
  1197.         if (i.regs[0]->reg_num == 5)
  1198.           t->base_opcode |= 0x08;
  1199.       }
  1200.     else if (t->opcode_modifier & Modrm)
  1201.       {
  1202.         /* The opcode is completed (modulo t->extension_opcode which must
  1203.            be put into the modrm byte.
  1204.            Now, we make the modrm & index base bytes based on all the info
  1205.            we've collected. */
  1206.  
  1207.         /* i.reg_operands MUST be the number of real register operands;
  1208.            implicit registers do not count. */
  1209.         if (i.reg_operands == 2)
  1210.           {
  1211.         unsigned int source, dest;
  1212.         source = (i.types[0] & (Reg | SReg2 | SReg3 | Control | Debug | Test)) ? 0 : 1;
  1213.         dest = source + 1;
  1214.         i.rm.mode = 3;
  1215.         /* We must be careful to make sure that all
  1216.            segment/control/test/debug registers go into the i.rm.reg
  1217.            field (despite the whether they are source or destination
  1218.            operands). */
  1219.         if (i.regs[dest]->reg_type & (SReg2 | SReg3 | Control | Debug | Test))
  1220.           {
  1221.             i.rm.reg = i.regs[dest]->reg_num;
  1222.             i.rm.regmem = i.regs[source]->reg_num;
  1223.           }
  1224.         else
  1225.           {
  1226.             i.rm.reg = i.regs[source]->reg_num;
  1227.             i.rm.regmem = i.regs[dest]->reg_num;
  1228.           }
  1229.           }
  1230.         else
  1231.           {            /* if it's not 2 reg operands... */
  1232.         if (i.mem_operands)
  1233.           {
  1234.             unsigned int fake_zero_displacement = 0;
  1235.             unsigned int op = (i.types[0] & Mem) ? 0 : ((i.types[1] & Mem) ? 1 : 2);
  1236.  
  1237.             /* Encode memory operand into modrm byte and base index
  1238.                byte. */
  1239.  
  1240.             if (i.base_reg == esp && !i.index_reg)
  1241.               {
  1242.             /* <disp>(%esp) becomes two byte modrm with no index
  1243.                register. */
  1244.             i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
  1245.             i.rm.mode = mode_from_disp_size (i.types[op]);
  1246.             i.bi.base = ESP_REG_NUM;
  1247.             i.bi.index = NO_INDEX_REGISTER;
  1248.             i.bi.scale = 0;    /* Must be zero! */
  1249.               }
  1250.             else if (i.base_reg == ebp && !i.index_reg)
  1251.               {
  1252.             if (!(i.types[op] & Disp))
  1253.               {
  1254.                 /* Must fake a zero byte displacement.  There is
  1255.                    no direct way to code '(%ebp)' directly. */
  1256.                 fake_zero_displacement = 1;
  1257.                 /* fake_zero_displacement code does not set this. */
  1258.                 i.types[op] |= Disp8;
  1259.               }
  1260.             i.rm.mode = mode_from_disp_size (i.types[op]);
  1261.             i.rm.regmem = EBP_REG_NUM;
  1262.               }
  1263.             else if (!i.base_reg && (i.types[op] & BaseIndex))
  1264.               {
  1265.             /* There are three cases here.
  1266.                Case 1:  '<32bit disp>(,1)' -- indirect absolute.
  1267.                (Same as cases 2 & 3 with NO index register)
  1268.                Case 2:  <32bit disp> (,<index>) -- no base register with disp
  1269.                Case 3:  (, <index>)       --- no base register;
  1270.                no disp (must add 32bit 0 disp). */
  1271.             i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
  1272.             i.rm.mode = 0;    /* 32bit mode */
  1273.             i.bi.base = NO_BASE_REGISTER;
  1274.             i.types[op] &= ~Disp;
  1275.             i.types[op] |= Disp32;    /* Must be 32bit! */
  1276.             if (i.index_reg)
  1277.               {    /* case 2 or case 3 */
  1278.                 i.bi.index = i.index_reg->reg_num;
  1279.                 i.bi.scale = i.log2_scale_factor;
  1280.                 if (i.disp_operands == 0)
  1281.                   fake_zero_displacement = 1;    /* case 3 */
  1282.               }
  1283.             else
  1284.               {
  1285.                 i.bi.index = NO_INDEX_REGISTER;
  1286.                 i.bi.scale = 0;
  1287.               }
  1288.               }
  1289.             else if (i.disp_operands && !i.base_reg && !i.index_reg)
  1290.               {
  1291.             /* Operand is just <32bit disp> */
  1292.             i.rm.regmem = EBP_REG_NUM;
  1293.             i.rm.mode = 0;
  1294.             i.types[op] &= ~Disp;
  1295.             i.types[op] |= Disp32;
  1296.               }
  1297.             else
  1298.               {
  1299.             /* It's not a special case; rev'em up. */
  1300.             i.rm.regmem = i.base_reg->reg_num;
  1301.             i.rm.mode = mode_from_disp_size (i.types[op]);
  1302.             if (i.index_reg)
  1303.               {
  1304.                 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
  1305.                 i.bi.base = i.base_reg->reg_num;
  1306.                 i.bi.index = i.index_reg->reg_num;
  1307.                 i.bi.scale = i.log2_scale_factor;
  1308.                 if (i.base_reg == ebp && i.disp_operands == 0)
  1309.                   {    /* pace */
  1310.                 fake_zero_displacement = 1;
  1311.                 i.types[op] |= Disp8;
  1312.                 i.rm.mode = mode_from_disp_size (i.types[op]);
  1313.                   }
  1314.               }
  1315.               }
  1316.             if (fake_zero_displacement)
  1317.               {
  1318.             /* Fakes a zero displacement assuming that i.types[op]
  1319.                holds the correct displacement size. */
  1320.             exp = &disp_expressions[i.disp_operands++];
  1321.             i.disps[op] = exp;
  1322.             exp->X_op = O_constant;
  1323.             exp->X_add_number = 0;
  1324.             exp->X_add_symbol = (symbolS *) 0;
  1325.             exp->X_op_symbol = (symbolS *) 0;
  1326.               }
  1327.  
  1328.             /* Select the correct segment for the memory operand. */
  1329.             if (i.seg)
  1330.               {
  1331.             unsigned int seg_index;
  1332.             const seg_entry *default_seg;
  1333.  
  1334.             if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING)
  1335.               {
  1336.                 seg_index = (i.rm.mode << 3) | i.bi.base;
  1337.                 default_seg = two_byte_segment_defaults[seg_index];
  1338.               }
  1339.             else
  1340.               {
  1341.                 seg_index = (i.rm.mode << 3) | i.rm.regmem;
  1342.                 default_seg = one_byte_segment_defaults[seg_index];
  1343.               }
  1344.             /* If the specified segment is not the default, use an
  1345.                opcode prefix to select it */
  1346.             if (i.seg != default_seg)
  1347.               {
  1348.                 if (i.prefixes == MAX_PREFIXES)
  1349.                   {
  1350.                 as_bad ("%d prefixes given and %s segment override gives too many prefixes",
  1351.                     MAX_PREFIXES, i.seg->seg_name);
  1352.                 return;
  1353.                   }
  1354.                 i.prefix[i.prefixes++] = i.seg->seg_prefix;
  1355.               }
  1356.               }
  1357.           }
  1358.  
  1359.         /* Fill in i.rm.reg or i.rm.regmem field with register operand
  1360.            (if any) based on t->extension_opcode. Again, we must be
  1361.            careful to make sure that segment/control/debug/test
  1362.            registers are coded into the i.rm.reg field. */
  1363.         if (i.reg_operands)
  1364.           {
  1365.             unsigned int op =
  1366.             (i.types[0] & (Reg | SReg2 | SReg3 | Control | Debug | Test)) ? 0 :
  1367.             (i.types[1] & (Reg | SReg2 | SReg3 | Control | Debug | Test)) ? 1 : 2;
  1368.             /* If there is an extension opcode to put here, the
  1369.                register number must be put into the regmem field. */
  1370.             if (t->extension_opcode != None)
  1371.               i.rm.regmem = i.regs[op]->reg_num;
  1372.             else
  1373.               i.rm.reg = i.regs[op]->reg_num;
  1374.  
  1375.             /* Now, if no memory operand has set i.rm.mode = 0, 1, 2
  1376.                we must set it to 3 to indicate this is a register
  1377.                operand int the regmem field */
  1378.             if (!i.mem_operands)
  1379.               i.rm.mode = 3;
  1380.           }
  1381.  
  1382.         /* Fill in i.rm.reg field with extension opcode (if any). */
  1383.         if (t->extension_opcode != None)
  1384.           i.rm.reg = t->extension_opcode;
  1385.           }
  1386.       }
  1387.       }
  1388.   }
  1389.  
  1390.   /* Handle conversion of 'int $3' --> special int3 insn. */
  1391.   if (t->base_opcode == INT_OPCODE && i.imms[0]->X_add_number == 3)
  1392.     {
  1393.       t->base_opcode = INT3_OPCODE;
  1394.       i.imm_operands = 0;
  1395.     }
  1396.  
  1397.   /* We are ready to output the insn. */
  1398.   {
  1399.     register char *p;
  1400.  
  1401.     /* Output jumps. */
  1402.     if (t->opcode_modifier & Jump)
  1403.       {
  1404.     unsigned long n = i.disps[0]->X_add_number;
  1405.  
  1406.     if (i.disps[0]->X_op == O_constant)
  1407.       {
  1408.         if (fits_in_signed_byte (n))
  1409.           {
  1410.         p = frag_more (2);
  1411.         insn_size += 2;
  1412.         p[0] = t->base_opcode;
  1413.         p[1] = n;
  1414.           }
  1415. #if 0                /* leave out 16 bit jumps - pace */
  1416.         else if (fits_in_signed_word (n))
  1417.           {
  1418.         p = frag_more (4);
  1419.         insn_size += 4;
  1420.         p[0] = WORD_PREFIX_OPCODE;
  1421.         p[1] = t->base_opcode;
  1422.         md_number_to_chars (&p[2], (valueT) n, 2);
  1423.           }
  1424. #endif
  1425.         else
  1426.           {            /* It's an absolute dword displacement. */
  1427.         if (t->base_opcode == JUMP_PC_RELATIVE)
  1428.           {        /* pace */
  1429.             /* unconditional jump */
  1430.             p = frag_more (5);
  1431.             insn_size += 5;
  1432.             p[0] = (char) 0xe9;
  1433.             md_number_to_chars (&p[1], (valueT) n, 4);
  1434.           }
  1435.         else
  1436.           {
  1437.             /* conditional jump */
  1438.             p = frag_more (6);
  1439.             insn_size += 6;
  1440.             p[0] = TWO_BYTE_OPCODE_ESCAPE;
  1441.             p[1] = t->base_opcode + 0x10;
  1442.             md_number_to_chars (&p[2], (valueT) n, 4);
  1443.           }
  1444.           }
  1445.       }
  1446.     else
  1447.       {
  1448.         /* It's a symbol; end frag & setup for relax.
  1449.            Make sure there are more than 6 chars left in the current frag;
  1450.            if not we'll have to start a new one. */
  1451.         if (obstack_room (&frags) <= 6)
  1452.           {
  1453.         frag_wane (frag_now);
  1454.         frag_new (0);
  1455.           }
  1456.         p = frag_more (1);
  1457.         insn_size += 1;
  1458.         p[0] = t->base_opcode;
  1459.         frag_var (rs_machine_dependent,
  1460.               6,    /* 2 opcode/prefix + 4 displacement */
  1461.               1,
  1462.               ((unsigned char) *p == JUMP_PC_RELATIVE
  1463.                ? ENCODE_RELAX_STATE (UNCOND_JUMP, BYTE)
  1464.                : ENCODE_RELAX_STATE (COND_JUMP, BYTE)),
  1465.               i.disps[0]->X_add_symbol,
  1466.               (long) n, p);
  1467.       }
  1468.       }
  1469.     else if (t->opcode_modifier & (JumpByte | JumpDword))
  1470.       {
  1471.     int size = (t->opcode_modifier & JumpByte) ? 1 : 4;
  1472.     unsigned long n = i.disps[0]->X_add_number;
  1473.  
  1474.     if (fits_in_unsigned_byte (t->base_opcode))
  1475.       {
  1476.         FRAG_APPEND_1_CHAR (t->base_opcode);
  1477.         insn_size += 1;
  1478.       }
  1479.     else
  1480.       {
  1481.         p = frag_more (2);    /* opcode can be at most two bytes */
  1482.         insn_size += 2;
  1483.         /* put out high byte first: can't use md_number_to_chars! */
  1484.         *p++ = (t->base_opcode >> 8) & 0xff;
  1485.         *p = t->base_opcode & 0xff;
  1486.       }
  1487.  
  1488.     p = frag_more (size);
  1489.     insn_size += size;
  1490.     if (i.disps[0]->X_op == O_constant)
  1491.       {
  1492.         md_number_to_chars (p, (valueT) n, size);
  1493.         if (size == 1 && !fits_in_signed_byte (n))
  1494.           {
  1495.         as_bad ("loop/jecx only takes byte displacement; %lu shortened to %d",
  1496.             n, *p);
  1497.           }
  1498.       }
  1499.     else
  1500.       {
  1501.         fix_new_exp (frag_now, p - frag_now->fr_literal, size,
  1502.              i.disps[0], 1, reloc (size, 1, i.disp_reloc[0]));
  1503.  
  1504.       }
  1505.       }
  1506.     else if (t->opcode_modifier & JumpInterSegment)
  1507.       {
  1508.     p = frag_more (1 + 2 + 4);    /* 1 opcode; 2 segment; 4 offset */
  1509.     insn_size += 1 + 2 + 4;
  1510.     p[0] = t->base_opcode;
  1511.     if (i.imms[1]->X_op == O_constant)
  1512.       md_number_to_chars (p + 1, (valueT) i.imms[1]->X_add_number, 4);
  1513.     else
  1514.       fix_new_exp (frag_now, p + 1 - frag_now->fr_literal, 4,
  1515.                i.imms[1], 0, BFD_RELOC_32);
  1516.     if (i.imms[0]->X_op != O_constant)
  1517.       as_bad ("can't handle non absolute segment in long call/jmp");
  1518.     md_number_to_chars (p + 5, (valueT) i.imms[0]->X_add_number, 2);
  1519.       }
  1520.     else
  1521.       {
  1522.     /* Output normal instructions here. */
  1523.     unsigned char *q;
  1524.  
  1525.     /* First the prefix bytes. */
  1526.     for (q = i.prefix; q < i.prefix + i.prefixes; q++)
  1527.       {
  1528.         p = frag_more (1);
  1529.         insn_size += 1;
  1530.         md_number_to_chars (p, (valueT) *q, 1);
  1531.       }
  1532.  
  1533.     /* Now the opcode; be careful about word order here! */
  1534.     if (fits_in_unsigned_byte (t->base_opcode))
  1535.       {
  1536.         FRAG_APPEND_1_CHAR (t->base_opcode);
  1537.         insn_size += 1;
  1538.       }
  1539.     else if (fits_in_unsigned_word (t->base_opcode))
  1540.       {
  1541.         p = frag_more (2);
  1542.         insn_size += 2;
  1543.         /* put out high byte first: can't use md_number_to_chars! */
  1544.         *p++ = (t->base_opcode >> 8) & 0xff;
  1545.         *p = t->base_opcode & 0xff;
  1546.       }
  1547.     else
  1548.       {            /* opcode is either 3 or 4 bytes */
  1549.         if (t->base_opcode & 0xff000000)
  1550.           {
  1551.         p = frag_more (4);
  1552.         insn_size += 4;
  1553.         *p++ = (t->base_opcode >> 24) & 0xff;
  1554.           }
  1555.         else
  1556.           {
  1557.         p = frag_more (3);
  1558.         insn_size += 3;
  1559.           }
  1560.         *p++ = (t->base_opcode >> 16) & 0xff;
  1561.         *p++ = (t->base_opcode >> 8) & 0xff;
  1562.         *p = (t->base_opcode) & 0xff;
  1563.       }
  1564.  
  1565.     /* Now the modrm byte and base index byte (if present). */
  1566.     if (t->opcode_modifier & Modrm)
  1567.       {
  1568.         p = frag_more (1);
  1569.         insn_size += 1;
  1570.         /* md_number_to_chars (p, i.rm, 1); */
  1571.         md_number_to_chars (p,
  1572.                 (valueT) (i.rm.regmem << 0
  1573.                       | i.rm.reg << 3
  1574.                       | i.rm.mode << 6),
  1575.                 1);
  1576.         /* If i.rm.regmem == ESP (4) && i.rm.mode != Mode 3 (Register mode)
  1577.                    ==> need second modrm byte. */
  1578.         if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING && i.rm.mode != 3)
  1579.           {
  1580.         p = frag_more (1);
  1581.         insn_size += 1;
  1582.         /* md_number_to_chars (p, i.bi, 1); */
  1583.         md_number_to_chars (p, (valueT) (i.bi.base << 0
  1584.                          | i.bi.index << 3
  1585.                          | i.bi.scale << 6),
  1586.                     1);
  1587.           }
  1588.       }
  1589.  
  1590.     if (i.disp_operands)
  1591.       {
  1592.         register unsigned int n;
  1593.  
  1594.         for (n = 0; n < i.operands; n++)
  1595.           {
  1596.         if (i.disps[n])
  1597.           {
  1598.             if (i.disps[n]->X_op == O_constant)
  1599.               {
  1600.             if (i.types[n] & (Disp8 | Abs8))
  1601.               {
  1602.                 p = frag_more (1);
  1603.                 insn_size += 1;
  1604.                 md_number_to_chars (p,
  1605.                         (valueT) i.disps[n]->X_add_number,
  1606.                         1);
  1607.               }
  1608.             else if (i.types[n] & (Disp16 | Abs16))
  1609.               {
  1610.                 p = frag_more (2);
  1611.                 insn_size += 2;
  1612.                 md_number_to_chars (p,
  1613.                         (valueT) i.disps[n]->X_add_number,
  1614.                         2);
  1615.               }
  1616.             else
  1617.               {    /* Disp32|Abs32 */
  1618.                 p = frag_more (4);
  1619.                 insn_size += 4;
  1620.                 md_number_to_chars (p,
  1621.                         (valueT) i.disps[n]->X_add_number,
  1622.                         4);
  1623.               }
  1624.               }
  1625.             else
  1626.               {        /* not absolute_section */
  1627.             /* need a 32-bit fixup (don't support 8bit non-absolute disps) */
  1628.             p = frag_more (4);
  1629.             insn_size += 4;
  1630.             fix_new_exp (frag_now, p - frag_now->fr_literal, 4,
  1631.                         i.disps[n], 0, 
  1632.                         TC_RELOC(i.disp_reloc[n], BFD_RELOC_32));
  1633.               }
  1634.           }
  1635.           }
  1636.       }            /* end displacement output */
  1637.  
  1638.     /* output immediate */
  1639.     if (i.imm_operands)
  1640.       {
  1641.         register unsigned int n;
  1642.  
  1643.         for (n = 0; n < i.operands; n++)
  1644.           {
  1645.         if (i.imms[n])
  1646.           {
  1647.             if (i.imms[n]->X_op == O_constant)
  1648.               {
  1649.             if (i.types[n] & (Imm8 | Imm8S))
  1650.               {
  1651.                 p = frag_more (1);
  1652.                 insn_size += 1;
  1653.                 md_number_to_chars (p,
  1654.                         (valueT) i.imms[n]->X_add_number,
  1655.                         1);
  1656.               }
  1657.             else if (i.types[n] & Imm16)
  1658.               {
  1659.                 p = frag_more (2);
  1660.                 insn_size += 2;
  1661.                 md_number_to_chars (p,
  1662.                         (valueT) i.imms[n]->X_add_number,
  1663.                         2);
  1664.               }
  1665.             else
  1666.               {
  1667.                 p = frag_more (4);
  1668.                 insn_size += 4;
  1669.                 md_number_to_chars (p,
  1670.                         (valueT) i.imms[n]->X_add_number,
  1671.                         4);
  1672.               }
  1673.               }
  1674.             else
  1675.               {        /* not absolute_section */
  1676.             /* Need a 32-bit fixup (don't support 8bit
  1677.                non-absolute ims).  Try to support other
  1678.                sizes ... */
  1679.             int r_type;
  1680.             int size;
  1681.             int pcrel = 0;
  1682.  
  1683.             if (i.types[n] & (Imm8 | Imm8S))
  1684.               size = 1;
  1685.             else if (i.types[n] & Imm16)
  1686.               size = 2;
  1687.             else
  1688.               size = 4;
  1689.             r_type = reloc (size, 0, i.disp_reloc[0]);
  1690.             p = frag_more (size);
  1691.             insn_size += size;
  1692. #ifdef BFD_ASSEMBLER
  1693.             if (r_type == BFD_RELOC_32
  1694.                 && i.imms[n]->X_op == O_symbol
  1695.                 && GOT_symbol
  1696.                 && GOT_symbol == i.imms[n]->X_add_symbol)
  1697.               {
  1698.                 r_type = BFD_RELOC_386_GOTPC;
  1699.                 i.imms[n]->X_add_number += 3;
  1700.               }
  1701. #endif
  1702.             fix_new_exp (frag_now, p - frag_now->fr_literal, size,
  1703.                      i.imms[n], pcrel, r_type);
  1704.               }
  1705.           }
  1706.           }
  1707.       }            /* end immediate output */
  1708.       }
  1709.  
  1710. #ifdef DEBUG386
  1711.     if (flag_debug)
  1712.       {
  1713.     pi (line, &i);
  1714.       }
  1715. #endif /* DEBUG386 */
  1716.   }
  1717. }
  1718.  
  1719. /* Parse OPERAND_STRING into the i386_insn structure I.  Returns non-zero
  1720.    on error. */
  1721.  
  1722. static int
  1723. i386_operand (operand_string)
  1724.      char *operand_string;
  1725. {
  1726.   register char *op_string = operand_string;
  1727.  
  1728.   /* Address of '\0' at end of operand_string. */
  1729.   char *end_of_operand_string = operand_string + strlen (operand_string);
  1730.  
  1731.   /* Start and end of displacement string expression (if found). */
  1732.   char *displacement_string_start = NULL;
  1733.   char *displacement_string_end = NULL;
  1734.  
  1735.   /* We check for an absolute prefix (differentiating,
  1736.      for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
  1737.   if (*op_string == ABSOLUTE_PREFIX)
  1738.     {
  1739.       op_string++;
  1740.       i.types[this_operand] |= JumpAbsolute;
  1741.     }
  1742.  
  1743.   /* Check if operand is a register. */
  1744.   if (*op_string == REGISTER_PREFIX)
  1745.     {
  1746.       register reg_entry *r;
  1747.       if (!(r = parse_register (op_string)))
  1748.     {
  1749.       as_bad ("bad register name ('%s')", op_string);
  1750.       return 0;
  1751.     }
  1752.       /* Check for segment override, rather than segment register by
  1753.      searching for ':' after %<x>s where <x> = s, c, d, e, f, g. */
  1754.       if ((r->reg_type & (SReg2 | SReg3)) && op_string[3] == ':')
  1755.     {
  1756.       switch (r->reg_num)
  1757.         {
  1758.         case 0:
  1759.           i.seg = (seg_entry *) & es;
  1760.           break;
  1761.         case 1:
  1762.           i.seg = (seg_entry *) & cs;
  1763.           break;
  1764.         case 2:
  1765.           i.seg = (seg_entry *) & ss;
  1766.           break;
  1767.         case 3:
  1768.           i.seg = (seg_entry *) & ds;
  1769.           break;
  1770.         case 4:
  1771.           i.seg = (seg_entry *) & fs;
  1772.           break;
  1773.         case 5:
  1774.           i.seg = (seg_entry *) & gs;
  1775.           break;
  1776.         }
  1777.       op_string += 4;    /* skip % <x> s : */
  1778.       operand_string = op_string;    /* Pretend given string starts here. */
  1779.       if (!is_digit_char (*op_string) && !is_identifier_char (*op_string)
  1780.           && *op_string != '(' && *op_string != ABSOLUTE_PREFIX)
  1781.         {
  1782.           as_bad ("bad memory operand after segment override");
  1783.           return 0;
  1784.         }
  1785.       /* Handle case of %es:*foo. */
  1786.       if (*op_string == ABSOLUTE_PREFIX)
  1787.         {
  1788.           op_string++;
  1789.           i.types[this_operand] |= JumpAbsolute;
  1790.         }
  1791.       goto do_memory_reference;
  1792.     }
  1793.       i.types[this_operand] |= r->reg_type;
  1794.       i.regs[this_operand] = r;
  1795.       i.reg_operands++;
  1796.     }
  1797.   else if (*op_string == IMMEDIATE_PREFIX)
  1798.     {                /* ... or an immediate */
  1799.       char *save_input_line_pointer;
  1800.       segT exp_seg = 0;
  1801.       expressionS *exp;
  1802.  
  1803.       if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
  1804.     {
  1805.       as_bad ("only 1 or 2 immediate operands are allowed");
  1806.       return 0;
  1807.     }
  1808.  
  1809.       exp = &im_expressions[i.imm_operands++];
  1810.       i.imms[this_operand] = exp;
  1811.       save_input_line_pointer = input_line_pointer;
  1812.       input_line_pointer = ++op_string;    /* must advance op_string! */
  1813.       SKIP_WHITESPACE ();
  1814.       exp_seg = expression (exp);
  1815.       input_line_pointer = save_input_line_pointer;
  1816.  
  1817.       if (exp->X_op == O_absent)
  1818.     {
  1819.       /* missing or bad expr becomes absolute 0 */
  1820.       as_bad ("missing or invalid immediate expression '%s' taken as 0",
  1821.           operand_string);
  1822.       exp->X_op = O_constant;
  1823.       exp->X_add_number = 0;
  1824.       exp->X_add_symbol = (symbolS *) 0;
  1825.       exp->X_op_symbol = (symbolS *) 0;
  1826.       i.types[this_operand] |= Imm;
  1827.     }
  1828.       else if (exp->X_op == O_constant)
  1829.     {
  1830.       i.types[this_operand] |=
  1831.         smallest_imm_type ((unsigned long) exp->X_add_number);
  1832.     }
  1833. #ifdef OBJ_AOUT
  1834.       else if (exp_seg != text_section
  1835.            && exp_seg != data_section
  1836.            && exp_seg != bss_section
  1837.            && exp_seg != undefined_section
  1838. #ifdef BFD_ASSEMBLER
  1839.            && ! bfd_is_com_section (exp_seg)
  1840. #endif
  1841.            )
  1842.     {
  1843.     seg_unimplemented:
  1844.       as_bad ("Unimplemented segment type %d in parse_operand", exp_seg);
  1845.       return 0;
  1846.     }
  1847. #endif
  1848.       else
  1849.     {
  1850.       /* this is an address ==> 32bit */
  1851.       i.types[this_operand] |= Imm32;
  1852.     }
  1853.       /* shorten this type of this operand if the instruction wants
  1854.        * fewer bits than are present in the immediate.  The bit field
  1855.        * code can put out 'andb $0xffffff, %al', for example.   pace
  1856.        * also 'movw $foo,(%eax)'
  1857.        */
  1858.       switch (i.suffix)
  1859.     {
  1860.     case WORD_OPCODE_SUFFIX:
  1861.       i.types[this_operand] |= Imm16;
  1862.       break;
  1863.     case BYTE_OPCODE_SUFFIX:
  1864.       i.types[this_operand] |= Imm16 | Imm8 | Imm8S;
  1865.       break;
  1866.     }
  1867.     }
  1868.   else if (is_digit_char (*op_string) || is_identifier_char (*op_string)
  1869.        || *op_string == '(')
  1870.     {
  1871.       /* This is a memory reference of some sort. */
  1872.       register char *base_string;
  1873.       unsigned int found_base_index_form;
  1874.  
  1875.     do_memory_reference:
  1876.       if (i.mem_operands == MAX_MEMORY_OPERANDS)
  1877.     {
  1878.       as_bad ("more than 1 memory reference in instruction");
  1879.       return 0;
  1880.     }
  1881.       i.mem_operands++;
  1882.  
  1883.       /* Determine type of memory operand from opcode_suffix;
  1884.            no opcode suffix implies general memory references. */
  1885.       switch (i.suffix)
  1886.     {
  1887.     case BYTE_OPCODE_SUFFIX:
  1888.       i.types[this_operand] |= Mem8;
  1889.       break;
  1890.     case WORD_OPCODE_SUFFIX:
  1891.       i.types[this_operand] |= Mem16;
  1892.       break;
  1893.     case DWORD_OPCODE_SUFFIX:
  1894.     default:
  1895.       i.types[this_operand] |= Mem32;
  1896.     }
  1897.  
  1898.       /* Check for base index form.  We detect the base index form by
  1899.      looking for an ')' at the end of the operand, searching
  1900.      for the '(' matching it, and finding a REGISTER_PREFIX or ','
  1901.      after it. */
  1902.       base_string = end_of_operand_string - 1;
  1903.       found_base_index_form = 0;
  1904.       if (*base_string == ')')
  1905.     {
  1906.       unsigned int parens_balenced = 1;
  1907.       /* We've already checked that the number of left & right ()'s are
  1908.          equal, so this loop will not be infinite. */
  1909.       do
  1910.         {
  1911.           base_string--;
  1912.           if (*base_string == ')')
  1913.         parens_balenced++;
  1914.           if (*base_string == '(')
  1915.         parens_balenced--;
  1916.         }
  1917.       while (parens_balenced);
  1918.       base_string++;    /* Skip past '('. */
  1919.       if (*base_string == REGISTER_PREFIX || *base_string == ',')
  1920.         found_base_index_form = 1;
  1921.     }
  1922.  
  1923.       /* If we can't parse a base index register expression, we've found
  1924.      a pure displacement expression.  We set up displacement_string_start
  1925.      and displacement_string_end for the code below. */
  1926.       if (!found_base_index_form)
  1927.     {
  1928.       displacement_string_start = op_string;
  1929.       displacement_string_end = end_of_operand_string;
  1930.     }
  1931.       else
  1932.     {
  1933.       char *base_reg_name, *index_reg_name, *num_string;
  1934.       int num;
  1935.  
  1936.       i.types[this_operand] |= BaseIndex;
  1937.  
  1938.       /* If there is a displacement set-up for it to be parsed later. */
  1939.       if (base_string != op_string + 1)
  1940.         {
  1941.           displacement_string_start = op_string;
  1942.           displacement_string_end = base_string - 1;
  1943.         }
  1944.  
  1945.       /* Find base register (if any). */
  1946.       if (*base_string != ',')
  1947.         {
  1948.           base_reg_name = base_string++;
  1949.           /* skip past register name & parse it */
  1950.           while (isalpha (*base_string))
  1951.         base_string++;
  1952.           if (base_string == base_reg_name + 1)
  1953.         {
  1954.           as_bad ("can't find base register name after '(%c'",
  1955.               REGISTER_PREFIX);
  1956.           return 0;
  1957.         }
  1958.           END_STRING_AND_SAVE (base_string);
  1959.           if (!(i.base_reg = parse_register (base_reg_name)))
  1960.         {
  1961.           as_bad ("bad base register name ('%s')", base_reg_name);
  1962.           return 0;
  1963.         }
  1964.           RESTORE_END_STRING (base_string);
  1965.         }
  1966.  
  1967.       /* Now check seperator; must be ',' ==> index reg
  1968.                OR num ==> no index reg. just scale factor
  1969.                OR ')' ==> end. (scale factor = 1) */
  1970.       if (*base_string != ',' && *base_string != ')')
  1971.         {
  1972.           as_bad ("expecting ',' or ')' after base register in `%s'",
  1973.               operand_string);
  1974.           return 0;
  1975.         }
  1976.  
  1977.       /* There may index reg here; and there may be a scale factor. */
  1978.       if (*base_string == ',' && *(base_string + 1) == REGISTER_PREFIX)
  1979.         {
  1980.           index_reg_name = ++base_string;
  1981.           while (isalpha (*++base_string));
  1982.           END_STRING_AND_SAVE (base_string);
  1983.           if (!(i.index_reg = parse_register (index_reg_name)))
  1984.         {
  1985.           as_bad ("bad index register name ('%s')", index_reg_name);
  1986.           return 0;
  1987.         }
  1988.           RESTORE_END_STRING (base_string);
  1989.         }
  1990.  
  1991.       /* Check for scale factor. */
  1992.       if (*base_string == ',' && isdigit (*(base_string + 1)))
  1993.         {
  1994.           num_string = ++base_string;
  1995.           while (is_digit_char (*base_string))
  1996.         base_string++;
  1997.           if (base_string == num_string)
  1998.         {
  1999.           as_bad ("can't find a scale factor after ','");
  2000.           return 0;
  2001.         }
  2002.           END_STRING_AND_SAVE (base_string);
  2003.           /* We've got a scale factor. */
  2004.           if (!sscanf (num_string, "%d", &num))
  2005.         {
  2006.           as_bad ("can't parse scale factor from '%s'", num_string);
  2007.           return 0;
  2008.         }
  2009.           RESTORE_END_STRING (base_string);
  2010.           switch (num)
  2011.         {        /* must be 1 digit scale */
  2012.         case 1:
  2013.           i.log2_scale_factor = 0;
  2014.           break;
  2015.         case 2:
  2016.           i.log2_scale_factor = 1;
  2017.           break;
  2018.         case 4:
  2019.           i.log2_scale_factor = 2;
  2020.           break;
  2021.         case 8:
  2022.           i.log2_scale_factor = 3;
  2023.           break;
  2024.         default:
  2025.           as_bad ("expecting scale factor of 1, 2, 4, 8; got %d", num);
  2026.           return 0;
  2027.         }
  2028.         }
  2029.       else
  2030.         {
  2031.           if (!i.index_reg && *base_string == ',')
  2032.         {
  2033.           as_bad ("expecting index register or scale factor after ','; got '%c'",
  2034.               *(base_string + 1));
  2035.           return 0;
  2036.         }
  2037.         }
  2038.     }
  2039.  
  2040.       /* If there's an expression begining the operand, parse it,
  2041.      assuming displacement_string_start and displacement_string_end
  2042.      are meaningful. */
  2043.       if (displacement_string_start)
  2044.     {
  2045.       register expressionS *exp;
  2046.       segT exp_seg = 0;
  2047.       char *save_input_line_pointer;
  2048.       exp = &disp_expressions[i.disp_operands];
  2049.       i.disps[this_operand] = exp;
  2050.       i.disp_reloc[this_operand] = NO_RELOC;
  2051.       i.disp_operands++;
  2052.       save_input_line_pointer = input_line_pointer;
  2053.       input_line_pointer = displacement_string_start;
  2054.       END_STRING_AND_SAVE (displacement_string_end);
  2055.  
  2056.       {
  2057.         /*
  2058.          * We can have operands of the form
  2059.          *   <symbol>@GOTOFF+<nnn>
  2060.          * Take the easy way out here and copy everything
  2061.          * into a temporary buffer...
  2062.          */
  2063.         register char *cp;
  2064.         if (cp = strchr(input_line_pointer,'@')) {
  2065.           char tmpbuf[BUFSIZ];
  2066.           
  2067.           if(!GOT_symbol)
  2068.         GOT_symbol = symbol_find_or_make(GLOBAL_OFFSET_TABLE_NAME);
  2069.  
  2070.           if (strncmp(cp+1, "PLT", 3) == 0) {
  2071.         i.disp_reloc[this_operand] = BFD_RELOC_386_PLT32;
  2072.         *cp = '\0';
  2073.         strcpy(tmpbuf, input_line_pointer);
  2074.         strcat(tmpbuf, cp+1+3);
  2075.         *cp = '@';
  2076.           } else if (strncmp(cp+1, "GOTOFF", 6) == 0) {
  2077.         i.disp_reloc[this_operand] = BFD_RELOC_386_GOTOFF;
  2078.         *cp = '\0';
  2079.         strcpy(tmpbuf, input_line_pointer);
  2080.         strcat(tmpbuf, cp+1+6);
  2081.         *cp = '@';
  2082.           } else if (strncmp(cp+1, "GOT", 3) == 0) {
  2083.         i.disp_reloc[this_operand] = BFD_RELOC_386_GOT32;
  2084.         *cp = '\0';
  2085.         strcpy(tmpbuf, input_line_pointer);
  2086.         strcat(tmpbuf, cp+1+3);
  2087.         *cp = '@';
  2088.           } else
  2089.         as_bad("Bad reloc specifier '%s' in expression", cp+1);
  2090.           input_line_pointer = tmpbuf;
  2091.         }
  2092.       }
  2093.  
  2094.       exp_seg = expression (exp);
  2095.  
  2096. #ifdef BFD_ASSEMBLER
  2097.       /* We do this to make sure that the section symbol is in
  2098.          the symbol table.  We will ultimately change the relocation
  2099.          to be relative to the beginning of the section */
  2100.       if (i.disp_reloc[this_operand] == BFD_RELOC_386_GOTOFF)
  2101.         {
  2102.           if (S_IS_LOCAL(exp->X_add_symbol)
  2103.           && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section)
  2104.         section_symbol(exp->X_add_symbol->bsym->section);
  2105.           assert (exp->X_op == O_symbol);
  2106.           exp->X_op = O_subtract;
  2107.           exp->X_op_symbol = GOT_symbol;
  2108.           i.disp_reloc[this_operand] = BFD_RELOC_32;
  2109.         }
  2110. #endif
  2111.  
  2112.       if (*input_line_pointer)
  2113.         as_bad ("Ignoring junk '%s' after expression", input_line_pointer);
  2114.       RESTORE_END_STRING (displacement_string_end);
  2115.       input_line_pointer = save_input_line_pointer;
  2116.       if (exp->X_op == O_absent)
  2117.         {
  2118.           /* missing expr becomes absolute 0 */
  2119.           as_bad ("missing or invalid displacement '%s' taken as 0",
  2120.               operand_string);
  2121.           i.types[this_operand] |= (Disp | Abs);
  2122.           exp->X_op = O_constant;
  2123.           exp->X_add_number = 0;
  2124.           exp->X_add_symbol = (symbolS *) 0;
  2125.           exp->X_op_symbol = (symbolS *) 0;
  2126.         }
  2127.       else if (exp->X_op == O_constant)
  2128.         {
  2129.           i.types[this_operand] |= SMALLEST_DISP_TYPE (exp->X_add_number);
  2130.         }
  2131.       else if (exp_seg == text_section
  2132.            || exp_seg == data_section
  2133.            || exp_seg == bss_section
  2134.            || exp_seg == undefined_section)
  2135.         {
  2136.           i.types[this_operand] |= Disp32;
  2137.         }
  2138.       else
  2139.         {
  2140. #ifndef OBJ_AOUT
  2141.           i.types[this_operand] |= Disp32;
  2142. #else
  2143.           goto seg_unimplemented;
  2144. #endif
  2145.         }
  2146.     }
  2147.  
  2148.       /* Make sure the memory operand we've been dealt is valid. */
  2149.       if (i.base_reg && i.index_reg &&
  2150.       !(i.base_reg->reg_type & i.index_reg->reg_type & Reg))
  2151.     {
  2152.       as_bad ("register size mismatch in (base,index,scale) expression");
  2153.       return 0;
  2154.     }
  2155.       /*
  2156.        * special case for (%dx) while doing input/output op
  2157.        */
  2158.       if ((i.base_reg &&
  2159.        (i.base_reg->reg_type == (Reg16 | InOutPortReg)) &&
  2160.        (i.index_reg == 0)))
  2161.     {
  2162.       i.types[this_operand] |= InOutPortReg;
  2163.       return 1;
  2164.     }
  2165.       if ((i.base_reg && (i.base_reg->reg_type & Reg32) == 0) ||
  2166.       (i.index_reg && (i.index_reg->reg_type & Reg32) == 0))
  2167.     {
  2168.       as_bad ("base/index register must be 32 bit register");
  2169.       return 0;
  2170.     }
  2171.       if (i.index_reg && i.index_reg == esp)
  2172.     {
  2173.       as_bad ("%s may not be used as an index register", esp->reg_name);
  2174.       return 0;
  2175.     }
  2176.     }
  2177.   else
  2178.     {                /* it's not a memory operand; argh! */
  2179.       as_bad ("invalid char %s begining %s operand '%s'",
  2180.           output_invalid (*op_string), ordinal_names[this_operand],
  2181.           op_string);
  2182.       return 0;
  2183.     }
  2184.   return 1;            /* normal return */
  2185. }
  2186.  
  2187. /*
  2188.  *            md_estimate_size_before_relax()
  2189.  *
  2190.  * Called just before relax().
  2191.  * Any symbol that is now undefined will not become defined.
  2192.  * Return the correct fr_subtype in the frag.
  2193.  * Return the initial "guess for fr_var" to caller.
  2194.  * The guess for fr_var is ACTUALLY the growth beyond fr_fix.
  2195.  * Whatever we do to grow fr_fix or fr_var contributes to our returned value.
  2196.  * Although it may not be explicit in the frag, pretend fr_var starts with a
  2197.  * 0 value.
  2198.  */
  2199. int
  2200. md_estimate_size_before_relax (fragP, segment)
  2201.      register fragS *fragP;
  2202.      register segT segment;
  2203. {
  2204.   register unsigned char *opcode;
  2205.   register int old_fr_fix;
  2206.  
  2207.   old_fr_fix = fragP->fr_fix;
  2208.   opcode = (unsigned char *) fragP->fr_opcode;
  2209.   /* We've already got fragP->fr_subtype right;  all we have to do is check
  2210.        for un-relaxable symbols. */
  2211.   if (S_GET_SEGMENT (fragP->fr_symbol) != segment)
  2212.     {
  2213.       /* symbol is undefined in this segment */
  2214.       switch (opcode[0])
  2215.     {
  2216.     case JUMP_PC_RELATIVE:    /* make jmp (0xeb) a dword displacement jump */
  2217.       opcode[0] = 0xe9;    /* dword disp jmp */
  2218.       fragP->fr_fix += 4;
  2219.       fix_new (fragP, old_fr_fix, 4,
  2220.                  fragP->fr_symbol,
  2221.            fragP->fr_offset, 1,
  2222.            (GOT_symbol && /* Not quite right - we should switch on
  2223.                      presence of @PLT, but I cannot see how
  2224.                      to get to that from here.  We should have
  2225.                      done this in md_assemble to really
  2226.                      get it right all of the time, but I
  2227.                      think it does not matter that much, as
  2228.                      this will be right most of the time. ERY*/
  2229.             S_GET_SEGMENT(fragP->fr_symbol) == undefined_section)?
  2230.            BFD_RELOC_386_PLT32 : BFD_RELOC_32_PCREL);
  2231.       break;
  2232.  
  2233.     default:
  2234.       /* This changes the byte-displacement jump 0x7N -->
  2235.                the dword-displacement jump 0x0f8N */
  2236.       opcode[1] = opcode[0] + 0x10;
  2237.       opcode[0] = TWO_BYTE_OPCODE_ESCAPE;    /* two-byte escape */
  2238.       fragP->fr_fix += 1 + 4;    /* we've added an opcode byte */
  2239.       fix_new (fragP, old_fr_fix + 1, 4,
  2240.            fragP->fr_symbol,
  2241.            fragP->fr_offset, 1, 
  2242.            (GOT_symbol &&  /* Not quite right - we should switch on
  2243.                      presence of @PLT, but I cannot see how
  2244.                      to get to that from here.  ERY */
  2245.             S_GET_SEGMENT(fragP->fr_symbol) == undefined_section)?
  2246.            BFD_RELOC_386_PLT32 : BFD_RELOC_32_PCREL);
  2247.       break;
  2248.     }
  2249.       frag_wane (fragP);
  2250.     }
  2251.   return (fragP->fr_var + fragP->fr_fix - old_fr_fix);
  2252. }                /* md_estimate_size_before_relax() */
  2253.  
  2254. /*
  2255.  *            md_convert_frag();
  2256.  *
  2257.  * Called after relax() is finished.
  2258.  * In:    Address of frag.
  2259.  *    fr_type == rs_machine_dependent.
  2260.  *    fr_subtype is what the address relaxed to.
  2261.  *
  2262.  * Out:    Any fixSs and constants are set up.
  2263.  *    Caller will turn frag into a ".space 0".
  2264.  */
  2265. #ifndef BFD_ASSEMBLER
  2266. void
  2267. md_convert_frag (headers, fragP)
  2268.      object_headers *headers;
  2269.      register fragS *fragP;
  2270. #else
  2271. void
  2272. md_convert_frag (abfd, sec, fragP)
  2273.      bfd *abfd;
  2274.      segT sec;
  2275.      register fragS *fragP;
  2276. #endif
  2277. {
  2278.   register unsigned char *opcode;
  2279.   unsigned char *where_to_put_displacement = NULL;
  2280.   unsigned int target_address;
  2281.   unsigned int opcode_address;
  2282.   unsigned int extension = 0;
  2283.   int displacement_from_opcode_start;
  2284.  
  2285.   opcode = (unsigned char *) fragP->fr_opcode;
  2286.  
  2287.   /* Address we want to reach in file space. */
  2288.   target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
  2289. #ifdef BFD_ASSEMBLER /* not needed otherwise? */
  2290.   target_address += fragP->fr_symbol->sy_frag->fr_address;
  2291. #endif
  2292.  
  2293.   /* Address opcode resides at in file space. */
  2294.   opcode_address = fragP->fr_address + fragP->fr_fix;
  2295.  
  2296.   /* Displacement from opcode start to fill into instruction. */
  2297.   displacement_from_opcode_start = target_address - opcode_address;
  2298.  
  2299.   switch (fragP->fr_subtype)
  2300.     {
  2301.     case ENCODE_RELAX_STATE (COND_JUMP, BYTE):
  2302.     case ENCODE_RELAX_STATE (UNCOND_JUMP, BYTE):
  2303.       /* don't have to change opcode */
  2304.       extension = 1;        /* 1 opcode + 1 displacement */
  2305.       where_to_put_displacement = &opcode[1];
  2306.       break;
  2307.  
  2308.     case ENCODE_RELAX_STATE (COND_JUMP, WORD):
  2309.       opcode[1] = TWO_BYTE_OPCODE_ESCAPE;
  2310.       opcode[2] = opcode[0] + 0x10;
  2311.       opcode[0] = WORD_PREFIX_OPCODE;
  2312.       extension = 4;        /* 3 opcode + 2 displacement */
  2313.       where_to_put_displacement = &opcode[3];
  2314.       break;
  2315.  
  2316.     case ENCODE_RELAX_STATE (UNCOND_JUMP, WORD):
  2317.       opcode[1] = 0xe9;
  2318.       opcode[0] = WORD_PREFIX_OPCODE;
  2319.       extension = 3;        /* 2 opcode + 2 displacement */
  2320.       where_to_put_displacement = &opcode[2];
  2321.       break;
  2322.  
  2323.     case ENCODE_RELAX_STATE (COND_JUMP, DWORD):
  2324.       opcode[1] = opcode[0] + 0x10;
  2325.       opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
  2326.       extension = 5;        /* 2 opcode + 4 displacement */
  2327.       where_to_put_displacement = &opcode[2];
  2328.       break;
  2329.  
  2330.     case ENCODE_RELAX_STATE (UNCOND_JUMP, DWORD):
  2331.       opcode[0] = 0xe9;
  2332.       extension = 4;        /* 1 opcode + 4 displacement */
  2333.       where_to_put_displacement = &opcode[1];
  2334.       break;
  2335.  
  2336.     default:
  2337.       BAD_CASE (fragP->fr_subtype);
  2338.       break;
  2339.     }
  2340.   /* now put displacement after opcode */
  2341.   md_number_to_chars ((char *) where_to_put_displacement,
  2342.               (valueT) (displacement_from_opcode_start - extension),
  2343.               SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
  2344.   fragP->fr_fix += extension;
  2345. }
  2346.  
  2347.  
  2348. int md_short_jump_size = 2;    /* size of byte displacement jmp */
  2349. int md_long_jump_size = 5;    /* size of dword displacement jmp */
  2350. const int md_reloc_size = 8;    /* Size of relocation record */
  2351.  
  2352. void
  2353. md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
  2354.      char *ptr;
  2355.      addressT from_addr, to_addr;
  2356.      fragS *frag;
  2357.      symbolS *to_symbol;
  2358. {
  2359.   long offset;
  2360.  
  2361.   offset = to_addr - (from_addr + 2);
  2362.   md_number_to_chars (ptr, (valueT) 0xeb, 1);    /* opcode for byte-disp jump */
  2363.   md_number_to_chars (ptr + 1, (valueT) offset, 1);
  2364. }
  2365.  
  2366. void
  2367. md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
  2368.      char *ptr;
  2369.      addressT from_addr, to_addr;
  2370.      fragS *frag;
  2371.      symbolS *to_symbol;
  2372. {
  2373.   long offset;
  2374.  
  2375.   if (flag_do_long_jump)
  2376.     {
  2377.       offset = to_addr - S_GET_VALUE (to_symbol);
  2378.       md_number_to_chars (ptr, (valueT) 0xe9, 1);/* opcode for long jmp */
  2379.       md_number_to_chars (ptr + 1, (valueT) offset, 4);
  2380.       fix_new (frag, (ptr + 1) - frag->fr_literal, 4,
  2381.            to_symbol, (offsetT) 0, 0, BFD_RELOC_32);
  2382.     }
  2383.   else
  2384.     {
  2385.       offset = to_addr - (from_addr + 5);
  2386.       md_number_to_chars (ptr, (valueT) 0xe9, 1);
  2387.       md_number_to_chars (ptr + 1, (valueT) offset, 4);
  2388.     }
  2389. }
  2390.  
  2391. void                /* Knows about order of bytes in address. */
  2392. md_number_to_chars (con, value, nbytes)
  2393.      char con[];        /* Return 'nbytes' of chars here. */
  2394.      valueT value;        /* The value of the bits. */
  2395.      int nbytes;        /* Number of bytes in the output. */
  2396. {
  2397.   number_to_chars_littleendian (con, value, nbytes);
  2398. }
  2399.  
  2400.  
  2401. /* Apply a fixup (fixS) to segment data, once it has been determined
  2402.    by our caller that we have all the info we need to fix it up.
  2403.  
  2404.    On the 386, immediates, displacements, and data pointers are all in
  2405.    the same (little-endian) format, so we don't need to care about which
  2406.    we are handling.  */
  2407.  
  2408. static void
  2409. md_apply_fix_1 (fixP, value)
  2410.      fixS *fixP;        /* The fix we're to put in */
  2411.      long value;        /* The value of the bits. */
  2412. {
  2413.   register char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
  2414.  
  2415. #if defined (BFD_ASSEMBLER) && !defined (TE_Mach)
  2416.   /*
  2417.    * This is a hack.  There should be a better way to
  2418.    * handle this.
  2419.    */
  2420.   if (fixP->fx_r_type == BFD_RELOC_32_PCREL && fixP->fx_addsy)
  2421.     {
  2422.       value += fixP->fx_where + fixP->fx_frag->fr_address;
  2423. #ifdef OBJ_ELF
  2424.       if (S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
  2425.     {
  2426.       /* Yes, we add the values in twice.  This is because
  2427.          bfd_perform_relocation subtracts them out again.  I think
  2428.          bfd_perform_relocation is broken, but I don't dare change
  2429.          it.  FIXME.  */
  2430.       value += fixP->fx_where + fixP->fx_frag->fr_address;
  2431.     }
  2432. #endif
  2433.     }
  2434.  
  2435.   /* Fix a few things - the dynamic linker expects certain values here,
  2436.      and we must not dissappoint it. */
  2437. #ifdef OBJ_ELF
  2438.   if (fixP->fx_addsy)
  2439.     switch(fixP->fx_r_type) {
  2440.     case BFD_RELOC_386_PLT32:
  2441.       /* Make the jump instruction point to the address of the operand.  At
  2442.      runtime we merely add the offset to the actual PLT entry. */
  2443.       value = 0xfffffffc;
  2444.       break;
  2445.     case BFD_RELOC_386_GOTPC:
  2446. /*
  2447.  *  This is tough to explain.  We end up with this one if we have
  2448.  * operands that look like "_GLOBAL_OFFSET_TABLE_+[.-.L284]".  The goal
  2449.  * here is to obtain the absolute address of the GOT, and it is strongly
  2450.  * preferable from a performance point of view to avoid using a runtime
  2451.  * relocation for this.  The actual sequence of instructions often look 
  2452.  * something like:
  2453.  * 
  2454.  *     call    .L66
  2455.  * .L66:
  2456.  *     popl    %ebx
  2457.  *     addl    $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
  2458.  * 
  2459.  *     The call and pop essentially return the absolute address of
  2460.  * the label .L66 and store it in %ebx.  The linker itself will
  2461.  * ultimately change the first operand of the addl so that %ebx points to
  2462.  * the GOT, but to keep things simple, the .o file must have this operand
  2463.  * set so that it generates not the absolute address of .L66, but the
  2464.  * absolute address of itself.  This allows the linker itself simply
  2465.  * treat a GOTPC relocation as asking for a pcrel offset to the GOT to be
  2466.  * added in, and the addend of the relocation is stored in the operand
  2467.  * field for the instruction itself.
  2468.  * 
  2469.  *     Our job here is to fix the operand so that it would add the correct
  2470.  * offset so that %ebx would point to itself.  The thing that is tricky is
  2471.  * that .-.L66 will point to the beginning of the instruction, so we need
  2472.  * to further modify the operand so that it will point to itself.
  2473.  * There are other cases where you have something like:
  2474.  * 
  2475.  *     .long    $_GLOBAL_OFFSET_TABLE_+[.-.L66]
  2476.  * 
  2477.  * and here no correction would be required.  Internally in the assembler
  2478.  * we treat operands of this form as not being pcrel since the '.' is 
  2479.  * explicitly mentioned, and I wonder whether it would simplify matters
  2480.  * to do it this way.  Who knows.  In earlier versions of the PIC patches,
  2481.  * the pcrel_adjust field was used to store the correction, but since the
  2482.  * expression is not pcrel, I felt it would be confusing to do it this way.
  2483.  */
  2484.       value -= 1;
  2485.       break;
  2486.     case BFD_RELOC_386_GOT32:
  2487.       value = 0; /* Fully resolved at runtime.  No addend. */
  2488.       break;
  2489.     case BFD_RELOC_386_GOTOFF:
  2490.       break;
  2491.  
  2492.     default:
  2493.       break;
  2494.     }
  2495. #endif
  2496.  
  2497. #endif
  2498.   md_number_to_chars (p, value, fixP->fx_size);
  2499. }
  2500.  
  2501. #ifdef BFD_ASSEMBLER
  2502. int
  2503. md_apply_fix (fixP, valp)
  2504.      fixS *fixP;
  2505.      valueT *valp;
  2506. {
  2507.   md_apply_fix_1 (fixP, *valp);
  2508.   return 1;
  2509. }
  2510. #else
  2511. void
  2512. md_apply_fix (fixP, val)
  2513.      fixS *fixP;
  2514.      long val;
  2515. {
  2516.   md_apply_fix_1 (fixP, val);
  2517. }
  2518. #endif
  2519.  
  2520. #if 0
  2521. /* This is never used.  */
  2522. long                /* Knows about the byte order in a word. */
  2523. md_chars_to_number (con, nbytes)
  2524.      unsigned char con[];    /* Low order byte 1st. */
  2525.      int nbytes;        /* Number of bytes in the input. */
  2526. {
  2527.   long retval;
  2528.   for (retval = 0, con += nbytes - 1; nbytes--; con--)
  2529.     {
  2530.       retval <<= BITS_PER_CHAR;
  2531.       retval |= *con;
  2532.     }
  2533.   return retval;
  2534. }
  2535. #endif /* 0 */
  2536.  
  2537.  
  2538. #define MAX_LITTLENUMS 6
  2539.  
  2540. /* Turn the string pointed to by litP into a floating point constant of type
  2541.    type, and emit the appropriate bytes.  The number of LITTLENUMS emitted
  2542.    is stored in *sizeP .  An error message is returned, or NULL on OK.  */
  2543. char *
  2544. md_atof (type, litP, sizeP)
  2545.      char type;
  2546.      char *litP;
  2547.      int *sizeP;
  2548. {
  2549.   int prec;
  2550.   LITTLENUM_TYPE words[MAX_LITTLENUMS];
  2551.   LITTLENUM_TYPE *wordP;
  2552.   char *t;
  2553.  
  2554.   switch (type)
  2555.     {
  2556.     case 'f':
  2557.     case 'F':
  2558.       prec = 2;
  2559.       break;
  2560.  
  2561.     case 'd':
  2562.     case 'D':
  2563.       prec = 4;
  2564.       break;
  2565.  
  2566.     case 'x':
  2567.     case 'X':
  2568.       prec = 5;
  2569.       break;
  2570.  
  2571.     default:
  2572.       *sizeP = 0;
  2573.       return "Bad call to md_atof ()";
  2574.     }
  2575.   t = atof_ieee (input_line_pointer, type, words);
  2576.   if (t)
  2577.     input_line_pointer = t;
  2578.  
  2579.   *sizeP = prec * sizeof (LITTLENUM_TYPE);
  2580.   /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
  2581.      the bigendian 386.  */
  2582.   for (wordP = words + prec - 1; prec--;)
  2583.     {
  2584.       md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
  2585.       litP += sizeof (LITTLENUM_TYPE);
  2586.     }
  2587.   return 0;
  2588. }
  2589.  
  2590. char output_invalid_buf[8];
  2591.  
  2592. static char *
  2593. output_invalid (c)
  2594.      char c;
  2595. {
  2596.   if (isprint (c))
  2597.     sprintf (output_invalid_buf, "'%c'", c);
  2598.   else
  2599.     sprintf (output_invalid_buf, "(0x%x)", (unsigned) c);
  2600.   return output_invalid_buf;
  2601. }
  2602.  
  2603. /* reg_string starts *before* REGISTER_PREFIX */
  2604. static reg_entry *
  2605. parse_register (reg_string)
  2606.      char *reg_string;
  2607. {
  2608.   register char *s = reg_string;
  2609.   register char *p;
  2610.   char reg_name_given[MAX_REG_NAME_SIZE];
  2611.  
  2612.   s++;                /* skip REGISTER_PREFIX */
  2613.   for (p = reg_name_given; is_register_char (*s); p++, s++)
  2614.     {
  2615.       *p = register_chars[(unsigned char) *s];
  2616.       if (p >= reg_name_given + MAX_REG_NAME_SIZE)
  2617.     return (reg_entry *) 0;
  2618.     }
  2619.   *p = '\0';
  2620.   return (reg_entry *) hash_find (reg_hash, reg_name_given);
  2621. }
  2622.  
  2623. #ifdef OBJ_ELF
  2624. CONST char *md_shortopts = "mVQ:";
  2625. #else
  2626. CONST char *md_shortopts = "m";
  2627. #endif
  2628. struct option md_longopts[] = {
  2629.   {NULL, no_argument, NULL, 0}
  2630. };
  2631. size_t md_longopts_size = sizeof(md_longopts);
  2632.  
  2633. int
  2634. md_parse_option (c, arg)
  2635.      int c;
  2636.      char *arg;
  2637. {
  2638.   switch (c)
  2639.     {
  2640.     case 'm':
  2641.       flag_do_long_jump = 1;
  2642.       break;
  2643.  
  2644. #ifdef OBJ_ELF
  2645.       /* -V: SVR4 argument to print version ID.  */
  2646.     case 'V':
  2647.       print_version_id ();
  2648.       break;
  2649.  
  2650.       /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
  2651.      should be emitted or not.  FIXME: Not implemented.  */
  2652.     case 'Q':
  2653.       break;
  2654. #endif
  2655.  
  2656.     default:
  2657.       return 0;
  2658.     }
  2659.   return 1;
  2660. }
  2661.  
  2662. void
  2663. md_show_usage (stream)
  2664.      FILE *stream;
  2665. {
  2666.   fprintf (stream, "\
  2667. -m            do long jump\n");
  2668. }
  2669.  
  2670. /* We have no need to default values of symbols.  */
  2671.  
  2672. /* ARGSUSED */
  2673. symbolS *
  2674. md_undefined_symbol (name)
  2675.      char *name;
  2676. {
  2677.     if (*name == '_' && *(name+1) == 'G'
  2678.         && strcmp(name, GLOBAL_OFFSET_TABLE_NAME) == 0)
  2679.       {
  2680.         if(!GOT_symbol)
  2681.           {
  2682.         if(symbol_find(name)) 
  2683.           as_bad("GOT already in symbol table");
  2684.         GOT_symbol = symbol_new (name, undefined_section, 
  2685.                      (valueT) 0, &zero_address_frag);
  2686.           };
  2687.         return GOT_symbol;
  2688.       }
  2689.   return 0;
  2690. }
  2691.  
  2692. /* Parse an operand that is machine-specific.
  2693.    We just return without modifying the expression if we have nothing
  2694.    to do.  */
  2695.  
  2696. /* ARGSUSED */
  2697. void
  2698. md_operand (expressionP)
  2699.      expressionS *expressionP;
  2700. {
  2701. }
  2702.  
  2703. /* Round up a section size to the appropriate boundary.  */
  2704. valueT
  2705. md_section_align (segment, size)
  2706.      segT segment;
  2707.      valueT size;
  2708. {
  2709.   return size;            /* Byte alignment is fine */
  2710. }
  2711.  
  2712. /* Exactly what point is a PC-relative offset relative TO?  On the
  2713.    i386, they're relative to the address of the offset, plus its
  2714.    size. (??? Is this right?  FIXME-SOON!) */
  2715. long
  2716. md_pcrel_from (fixP)
  2717.      fixS *fixP;
  2718. {
  2719.   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
  2720. }
  2721.  
  2722. #ifndef I386COFF
  2723.  
  2724. static void
  2725. s_bss (ignore)
  2726.      int ignore;
  2727. {
  2728.   register int temp;
  2729.  
  2730.   temp = get_absolute_expression ();
  2731.   subseg_set (bss_section, (subsegT) temp);
  2732.   demand_empty_rest_of_line ();
  2733. }
  2734.  
  2735. #endif
  2736.  
  2737.  
  2738. #ifdef BFD_ASSEMBLER
  2739.  
  2740. void
  2741. i386_validate_fix (fixp)
  2742.      fixS *fixp;
  2743. {
  2744.   if (fixp->fx_subsy && fixp->fx_subsy == GOT_symbol)
  2745.     {
  2746.       fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
  2747.       fixp->fx_subsy = 0;
  2748.     }
  2749. }
  2750.  
  2751. #define F(SZ,PCREL)        (((SZ) << 1) + (PCREL))
  2752. #define MAP(SZ,PCREL,TYPE)    case F(SZ,PCREL): code = (TYPE); break
  2753.  
  2754. arelent *
  2755. tc_gen_reloc (section, fixp)
  2756.      asection *section;
  2757.      fixS *fixp;
  2758. {
  2759.   arelent *rel;
  2760.   bfd_reloc_code_real_type code;
  2761.  
  2762.   switch(fixp->fx_r_type)
  2763.     {
  2764.     case BFD_RELOC_386_PLT32:
  2765.     case BFD_RELOC_386_GOT32:
  2766.     case BFD_RELOC_386_GOTOFF:
  2767.     case BFD_RELOC_386_GOTPC:
  2768.       code = fixp->fx_r_type;
  2769.       break;
  2770.     default:
  2771.       switch (F (fixp->fx_size, fixp->fx_pcrel))
  2772.     {
  2773. #ifndef OBJ_ELF
  2774.       MAP (1, 0, BFD_RELOC_8);
  2775.       MAP (2, 0, BFD_RELOC_16);
  2776. #endif
  2777.       MAP (4, 0, BFD_RELOC_32);
  2778. #ifndef OBJ_ELF
  2779.       MAP (1, 1, BFD_RELOC_8_PCREL);
  2780.       MAP (2, 1, BFD_RELOC_16_PCREL);
  2781. #endif
  2782.       MAP (4, 1, BFD_RELOC_32_PCREL);
  2783.     default:
  2784.       as_bad ("Can not do %d byte %srelocation", fixp->fx_size,
  2785.           fixp->fx_pcrel ? "pc-relative" : "");
  2786.     }
  2787.     }
  2788. #undef MAP
  2789. #undef F
  2790.  
  2791.   if (code == BFD_RELOC_32
  2792.       && GOT_symbol
  2793.       && fixp->fx_addsy == GOT_symbol)
  2794.     code = BFD_RELOC_386_GOTPC;
  2795.  
  2796.   rel = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
  2797.   assert (rel != 0);
  2798.   rel->sym_ptr_ptr = &fixp->fx_addsy->bsym;
  2799.   rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
  2800.   if (fixp->fx_pcrel)
  2801.     rel->addend = fixp->fx_addnumber;
  2802.   else
  2803.     rel->addend = 0;
  2804.  
  2805.   rel->howto = bfd_reloc_type_lookup (stdoutput, code);
  2806.   if (!rel->howto)
  2807.     {
  2808.       const char *name;
  2809.  
  2810.       name = S_GET_NAME (fixp->fx_addsy);
  2811.       if (name == NULL)
  2812.     name = "<unknown>";
  2813.       as_fatal ("Cannot generate relocation type for symbol %s, code %s",
  2814.         name, bfd_get_reloc_code_name (code));
  2815.     }
  2816.  
  2817.   return rel;
  2818. }
  2819.  
  2820. #else /* ! BFD_ASSEMBLER */
  2821.  
  2822. #if (defined(OBJ_AOUT) | defined(OBJ_BOUT))
  2823. void
  2824. tc_aout_fix_to_chars (where, fixP, segment_address_in_file)
  2825.      char *where;
  2826.      fixS *fixP;
  2827.      relax_addressT segment_address_in_file;
  2828. {
  2829.   /*
  2830.    * In: length of relocation (or of address) in chars: 1, 2 or 4.
  2831.    * Out: GNU LD relocation length code: 0, 1, or 2.
  2832.    */
  2833.  
  2834.   static const unsigned char nbytes_r_length[] = {42, 0, 1, 42, 2};
  2835.   long r_symbolnum;
  2836.  
  2837.   know (fixP->fx_addsy != NULL);
  2838.  
  2839.   md_number_to_chars (where,
  2840.               (valueT) (fixP->fx_frag->fr_address
  2841.                 + fixP->fx_where - segment_address_in_file),
  2842.               4);
  2843.  
  2844.   r_symbolnum = (S_IS_DEFINED (fixP->fx_addsy)
  2845.          ? S_GET_TYPE (fixP->fx_addsy)
  2846.          : fixP->fx_addsy->sy_number);
  2847.  
  2848.   where[6] = (r_symbolnum >> 16) & 0x0ff;
  2849.   where[5] = (r_symbolnum >> 8) & 0x0ff;
  2850.   where[4] = r_symbolnum & 0x0ff;
  2851.   where[7] = ((((!S_IS_DEFINED (fixP->fx_addsy)) << 3) & 0x08)
  2852.           | ((nbytes_r_length[fixP->fx_size] << 1) & 0x06)
  2853.           | (((fixP->fx_pcrel << 0) & 0x01) & 0x0f));
  2854. }
  2855.  
  2856. #endif /* OBJ_AOUT or OBJ_BOUT */
  2857.  
  2858. #if defined (I386COFF)
  2859.  
  2860. short
  2861. tc_coff_fix2rtype (fixP)
  2862.      fixS *fixP;
  2863. {
  2864.   return (fixP->fx_pcrel ?
  2865.       (fixP->fx_size == 1 ? R_PCRBYTE :
  2866.        fixP->fx_size == 2 ? R_PCRWORD :
  2867.        R_PCRLONG) :
  2868.       (fixP->fx_size == 1 ? R_RELBYTE :
  2869.        fixP->fx_size == 2 ? R_RELWORD :
  2870.        R_DIR32));
  2871. }
  2872.  
  2873. int
  2874. tc_coff_sizemachdep (frag)
  2875.      fragS *frag;
  2876. {
  2877.   if (frag->fr_next)
  2878.     return (frag->fr_next->fr_address - frag->fr_address);
  2879.   else
  2880.     return 0;
  2881. }
  2882.  
  2883. #endif /* I386COFF */
  2884.  
  2885. #endif /* BFD_ASSEMBLER? */
  2886.  
  2887. /* end of tc-i386.c */
  2888.